Spring自定义环境配置(profile)
Spring3.1之后就支持在配置文件中使用profile,可以满足直接将dev环境、test环境、product环境的差异化配置都定义出来,然后在web.xml中指定使用哪个配置。
1.applicationContext.xml中头部使用beans,注意版本为3.1之后
<?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:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd ">
2.applicationContext.xml中使用profile定义
<bean id="applicationPropertyConfig" class="com.baidu.disconf.web.config.ApplicationPropertyConfig"> <property name="emailHost" value="${EMAIL_HOST}"/> <property name="emailPassword" value="${EMAIL_HOST_PASSWORD}"/> <property name="emailUser" value="${EMAIL_HOST_USER}"/> <property name="emailPort" value="${EMAIL_PORT}"/> <property name="fromEmail" value="${DEFAULT_FROM_EMAIL}"/> <property name="emailReceiver" value="${EMAIL_RECEIVER}"/> <property name="emailMonitorOn" value="${EMAIL_MONITOR_ON}"/> <property name="checkConsistencyOn" value="${CHECK_CONSISTENCY_ON}"/> <property name="domain" value="${domain}"/> </bean> <!-- import --> <import resource="classpath*:myconfig/applicationContext-cache.xml"/> <import resource="classpath*:myconfig/applicationContext-springcache.xml"/> <!-- profile --> <beans profile="db-test"> <import resource="classpath*:applicationContext-dbconfig-test.xml"/> <bean id="loginRedisLogin" class="com.baidu.disconf.web.web.auth.login.impl.RedisLoginImplMock"/> <bean id="roleResourceAspect" class="com.baidu.disconf.web.service.roleres.service.RoleResourceAspectMock"/> <bean id="zookeeperDriver" class="com.baidu.disconf.web.innerapi.zookeeper.impl.ZookeeperDriverMock"/> </beans> <beans profile="db-normal"> <import resource="classpath*:myconfig/applicationContext-dbconfig.xml"/> <bean id="loginRedisLogin" class="com.baidu.disconf.web.web.auth.login.impl.RedisLoginImpl"/> <bean id="roleResourceAspect" class="com.baidu.disconf.web.service.roleres.service.RoleResourceAspect"/> <bean id="zookeeperDriver" class="com.baidu.disconf.web.innerapi.zookeeper.impl.ZookeeperDriverImpl"/> </beans> <beans profile="production"> <import resource="classpath*:myconfig/applicationContext-dbconfig.xml"/> <bean id="loginRedisLogin" class="com.baidu.disconf.web.web.auth.login.impl.RedisLoginImpl"/> <bean id="roleResourceAspect" class="com.baidu.disconf.web.service.roleres.service.RoleResourceAspect"/> <bean id="zookeeperDriver" class="com.baidu.disconf.web.innerapi.zookeeper.impl.ZookeeperDriverImpl"/> </beans> <beans profile="rd"> <import resource="classpath*:myconfig/applicationContext-dbconfig.xml"/> <bean id="loginRedisLogin" class="com.baidu.disconf.web.web.auth.login.impl.RedisLoginImplMock"/> <bean id="roleResourceAspect" class="com.baidu.disconf.web.service.roleres.service.RoleResourceAspectMock"/> <bean id="zookeeperDriver" class="com.baidu.disconf.web.innerapi.zookeeper.impl.ZookeeperDriverMock"/> </beans>
<beans profile="profileName">
......
</beans>
注意,<beans>要最外层的<beans></beans>内部的最后面,否则,会报错。
3.web.xml中指定使用哪个profile
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>production</param-value>
</context-param>