利用spring 的profile环境配置能够区分不一样环境下的配置,但只能配置一个PropertyPlaceholderConfigurer,若是出现多个,后面的会覆盖前面的,致使配置找不到。web
配置文件的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 不一样的配置切换 --> <beans profile="dev"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:application-dev.properties</value> </list> </property> </bean> </beans> <beans profile="prod"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:application-prod.properties</value> </list> </property> </bean> </beans> </beans>
application-prod.properties
正式环境中的配置spring
application-dev.properties
测试环境中的配置app
切换环境
能够在web.xml中配置默认环境测试
<context-param> <param-name>spring.profiles.default</param-name> <param-value>prod</param-value> </context-param>
切换配置时配置spring.profiles.active变量的值,能够配置在环境变量中也能够配置配置文件中,建议配置到环境变量中。spa