spring配置文件加载



Spring配置文件是集成了Spring框架的项目的核心,引擎从哪里开始,中间都执行了哪些操作,小谈一下它的执行流程。

容器先是加载web.xml

 

接着是applicationContext.xml在web.xml里的注册

 

一种方法是加入ContextLoaderServlet这个servlet

 

 

复制代码
 1 <context-param>  
 2         <param-name>contextConfigLocation</param-name>  
 3         <param-value>/WEB-INF/applicationContext.xml</param-value>  
 4     </context-param>  
 5      <servlet>  
 6         <servlet-name>context</servlet-name>  
 7         <servlet-class>  
 8             org.springframework.web.context.ContextLoaderServlet   
 9         </servlet-class>  
10         <load-on-startup>0</load-on-startup>  
11     </servlet>  
复制代码

 

还有一种是添加ContextLoaderListener这个监听器

 

复制代码
1 <context-param>  
2     <param-name>contextConfigLocation</param-name>  
3     <param-value>/WEB-INF/applicationContext.xml</param-value>  
4 </context-param>  
5   
6 <listener>  
7     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
8 </listener>  
复制代码

 

 ContextLoaderServlet和ContextLoaderListener都是先创建ContextLoader的一个对象,然后调用它的initWebApplicationContex方法初始化WebApplicationContext获得一个对象;

 

spring加载多个配置文件,在web.xml中

 

复制代码
 1 <context-param>
 2         <param-name>contextConfigLocation</param-name>
 3         <param-value>classpath*:spring/*.xml</param-value>
 4 </context-param>
 5 
 6 <servlet>
 7         <servlet-name>SpringContextServlet</servlet-name>
 8         <servlet-class>
 9             org.springframework.web.context.ContextLoaderServlet
10         </servlet-class>
11         <load-on-startup>3</load-on-startup>
12 </servlet>
复制代码

 

 

Spring配置文件是集成了Spring框架的项目的核心,引擎从哪里开始,中间都执行了哪些操作,小谈一下它的执行流程。

容器先是加载web.xml

 

接着是applicationContext.xml在web.xml里的注册

 

一种方法是加入ContextLoaderServlet这个servlet

 

 

复制代码
 1 <context-param>  
 2         <param-name>contextConfigLocation</param-name>  
 3         <param-value>/WEB-INF/applicationContext.xml</param-value>  
 4     </context-param>  
 5      <servlet>  
 6         <servlet-name>context</servlet-name>  
 7         <servlet-class>  
 8             org.springframework.web.context.ContextLoaderServlet   
 9         </servlet-class>  
10         <load-on-startup>0</load-on-startup>  
11     </servlet>  
复制代码

 

还有一种是添加ContextLoaderListener这个监听器

 

复制代码
1 <context-param>  
2     <param-name>contextConfigLocation</param-name>  
3     <param-value>/WEB-INF/applicationContext.xml</param-value>  
4 </context-param>  
5   
6 <listener>  
7     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
8 </listener>  
复制代码

 

 ContextLoaderServlet和ContextLoaderListener都是先创建ContextLoader的一个对象,然后调用它的initWebApplicationContex方法初始化WebApplicationContext获得一个对象;

 

spring加载多个配置文件,在web.xml中

 

复制代码
 1 <context-param>
 2         <param-name>contextConfigLocation</param-name>
 3         <param-value>classpath*:spring/*.xml</param-value>
 4 </context-param>
 5 
 6 <servlet>
 7         <servlet-name>SpringContextServlet</servlet-name>
 8         <servlet-class>
 9             org.springframework.web.context.ContextLoaderServlet
10         </servlet-class>
11         <load-on-startup>3</load-on-startup>
12 </servlet>
复制代码

 

 


一、Spring配置文件在类路径下面

在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。

以下是我的项目,因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下


这时候,在代码中可以通过

[java] view plaincopy
  1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
然后获取相应的bean。


如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:

[java] view plaincopy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations={"classpath:applicationContext.xml"})  

只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。



二、Spring配置文件在WEB-INF下面

当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在

WEB-INF目录下面,如下图。


这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。

[java] view plaincopy
  1. ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");  

然后获取相应的bean。



如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:


[java] view plaincopy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})   
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。



下面是我的一个Spring管理下的Junit测试类:


[java] view plaincopy
  1. package com.sohu.group.service.external;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.junit.Test;  
  6. import org.junit.runner.RunWith;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.test.context.ContextConfiguration;  
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  10.   
  11. @RunWith(SpringJUnit4ClassRunner.class)  
  12. @ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})  
  13. public class SuFriendServiceImplOverRMITest {  
  14.   
  15.     @Autowired  
  16.     private SuFriendService suFriendService;  
  17.       
  18.     @Test  
  19.     public void getUserFollowerListTest(){  
  20.         List list = suFriendService.getUserFollowerList("liug_talk@163.com");  
  21.         System.out.println("------"+list);  
  22.     }  
  23. }  

原文地址:http://blog.youkuaiyun.com/hbccgg/article/details/6358591




Spring配置文件是集成了Spring框架的项目的核心,引擎从哪里开始,中间都执行了哪些操作,小谈一下它的执行流程。

容器先是加载web.xml

 

接着是applicationContext.xml在web.xml里的注册

 

一种方法是加入ContextLoaderServlet这个servlet

 

 

复制代码
 1 <context-param>  
 2         <param-name>contextConfigLocation</param-name>  
 3         <param-value>/WEB-INF/applicationContext.xml</param-value>  
 4     </context-param>  
 5      <servlet>  
 6         <servlet-name>context</servlet-name>  
 7         <servlet-class>  
 8             org.springframework.web.context.ContextLoaderServlet   
 9         </servlet-class>  
10         <load-on-startup>0</load-on-startup>  
11     </servlet>  
复制代码

 

还有一种是添加ContextLoaderListener这个监听器

 

复制代码
1 <context-param>  
2     <param-name>contextConfigLocation</param-name>  
3     <param-value>/WEB-INF/applicationContext.xml</param-value>  
4 </context-param>  
5   
6 <listener>  
7     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
8 </listener>  
复制代码

 

 ContextLoaderServlet和ContextLoaderListener都是先创建ContextLoader的一个对象,然后调用它的initWebApplicationContex方法初始化WebApplicationContext获得一个对象;

 

spring加载多个配置文件,在web.xml中

 

复制代码
 1 <context-param>
 2         <param-name>contextConfigLocation</param-name>
 3         <param-value>classpath*:spring/*.xml</param-value>
 4 </context-param>
 5 
 6 <servlet>
 7         <servlet-name>SpringContextServlet</servlet-name>
 8         <servlet-class>
 9             org.springframework.web.context.ContextLoaderServlet
10         </servlet-class>
11         <load-on-startup>3</load-on-startup>
12 </servlet>
复制代码

 

 

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <description>Spring公共配置文件</description> <!-- mes 的數據庫 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mestest"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="mesDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码和用户名 --> <bean id="mesDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- ptc windchill的數據庫 --> <bean id="dataSourcePdm" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mesdev"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="ptcDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码和用户名 --> <bean id="ptcDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- mes數據源代理 --> <bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy" p:targetDataSource-ref="dataSource"/> <!-- 对web包中的所有进行扫描,以完成Bean创建和自动依赖注入的功能--> <context:component-scan base-package="com.ccc"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /> <!-- 配置事务管理器 針對MES數據庫--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourceProxy"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.ccc..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config> <!-- 配置事务管理器,這個事務性是爭對pdm數據庫的 --> <bean id="transactionManagerPdm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourcePdm"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvicePdm" transaction-manager="transactionManagerPdm"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethodPdm" expression="execution(* com.ccc.pdm..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethodPdm" advice-ref="txAdvicePdm"/> </aop:config> <!-- ibatis插件 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- 配置要拦截的url,防止2次提交或做其他數據統計用 <bean id="doubleSubmitInterceptor" class="com.ccc.filter.DoubleSubmitInterceptor"> <property name="mappingURL" value=".html" /> <property name="viewURL" value=".html" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> </property> </bean> --> <!-- JDBC template注入及事務配置 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="dataSourceProxy"/></property> </bean> </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值