当
spring中同时存在多个配置文件时,你必须同时加载它,那你怎么办呢?我所知道的方法以下几种:
1.使用监听器或servlet,当然也可以自己写类实现
这种方法要使用多个配置文件只需在web.xml中加入一个
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/applicationContext.xml
</param-value>
</context-param>
多个配置文件用逗号或空格分开即可,你可以选择用以下几种方法读取值:
(1)
String relativePath = this.getServlettContext().getInitParameter(
"contextConfigLocation");
String[] ctxs = filePath.split(",");
ApplicationContext orderServiceContext = new
ClassPathXmlApplicationContext(ctxs);
(2)
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(super.getServletContext());
2.通过一个父配置文件将所有子配置文件导入。
在配置文件中有一个标签import,它能把其它的bean定义配置文件导入到父文件夹中,我就是用这种做的,下面粘贴出我测试用的代码:
web.xml配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/applicationContext.xml</param-value>
</context-param>
主配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- hibernate配置开始 -->
<import resource="spring_hibernate.xml" />
<!-- hibernate配置结束 -->
<!-- bean配置开始 -->
<import resource="spring_beans.xml" />
<!-- bean配置结束 -->
<!-- 事务配置开始 -->
<bean id="baseService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target">
<list>
<ref bean="test" />
</list>
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">readOnly,-Exception</prop>
</props>
</property>
</bean>
<!-- 事务配置结束 -->
<!-- 资源文件,国际化信息开始 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>com.wzw.messages.messages</value>
</list>
</property>
</bean>
<!-- 资源文件,国际化信息结束 -->
<!-- struts配置开始 -->
<import resource="spring_struts.xml" />
<!-- struts配置结束 -->
</beans>
spring_hibernate.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置数据库连接池开始-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<!-- 配置数据库连接池结束-->
<!-- hibernate配置开始 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/wzw/hibernate/hbm</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
<property name="jdbcExceptionTranslator" ref="jdbcExceptionTranslator" />
</bean>
<bean id="hibernateUtils" class="com.wzw.hibernate.core.HibernateUtils">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<!-- hibernate配置结束 -->
</beans>
spring_beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="test" class="com.wzw.daoImpl.testImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
spring_struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- struts配置开始 -->
<bean name="/user/useraccountaction" class="com.wzw.struts.action.UserAccountAction" singleton="false">
</bean>
<!-- struts配置结束 -->
</beans>
3.使用ClassPathApplicationContext。这个类会自动从classpath目录中加载所有的配置文件,不过这个类好像只适用本地调试时用的。
我想上面几种方法已经够用了,实在没必要去找别的了!