利用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