Spring 的配置

        1.Web项目引入Spring的方式。(web.xml的配置) 
        spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn
        spring3.0及以后版本中已经删除ContextLoaderServlet 和Log4jConfigServlet。可以采用余下两种启动方式ContextLoaderListener和ContextLoaderPlugIn。因为我们的COntextLoaderPlugIn需要导入org.springframework.web.struts-3.0.5.RELEASE.jar的包才能实现,也就是当你做struts和spring整合的时候(注意不是struts2),我们就可以用这个方式来做。所以我们建议使用ContextLoaderListener,配置如下:
 <listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener> 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
		<!-- <param-value>classpath:applicationContext*.xml</param-value> -->
	</context-param>

        要知道contextConfigLocation在 ContextLoaderListener类中的默认值是 /WEB-INF/applicationContext.xml。所以,如果不配置context-param中的contextConfigLocation的值,那么我们的spring就用默认的这个值。

        2.Spring中的容器管理配置。(applicationContext.xml的配置)
        我们先来一个空的Spring配置文件(资源利用,因为一般也不会去记XML前面的一大串命名空间啊,xsd文件神马的)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

</beans>

        我们来看一段关于数据源的部分配置信息。
<!--此bean用来告诉Spring去何处找数据库信息,有此Bean才会有下面dataSource中用${}标记来取变量的语句 --> 
<bean id="propertyConfig" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="location"> 
<value>classpath:jdbc.properties</value> //jdbc.properties为连接数据库的配置文件 
</property> 
</bean> 

<!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息 --> 
<bean id="dataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName"> 
<value>${driverClassName}</value> 
</property> 
<property name="url"> 
<value>${url}</value> 
</property> 
<property name="username"> 
<value>${username}</value> 
</property> 
<property name="password"> 
<value>${password}</value> 
</property> 
</bean> 

<!--根据dataSource和configLocation创建一个SqlMapClient --> 
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
<property name="configLocations"> 
<list> 
//sql语句配置xml文件 
<value>classpath:config/ibatisxxx/xxx/sql_map_config.xml</value> 
... 
</list> 
</property> 
<property name="dataSource"> 
<ref bean="dataSource" /> 
</property> 
</bean> 

<!--根据sqlMapClien创建一个SqlMapClient模版类 --> 
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> 
<property name="sqlMapClient"> 
<ref bean="sqlMapClient" /> 
</property> 
</bean> 

        AOP的配置,我这里就不写了,因为太多帖子是关于这个的了,输入关键字就能找到,这里推荐一个帖子:
             http://blog.csdn.net/yuqinying112/article/details/7335416

        通过上面这个列子,应该很容易知道如何配置AOP了吧。至于如何在xml里面配置管理和依赖注入,帖子也太多太多了,就不详细的记在这里了。

猜你喜欢

转载自goalietang.iteye.com/blog/2032611