1.建立一个配置文件设置环境变量
2.新建一个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 class="shelper.spring.ext.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="rootLocation"> <value>classpath:properties/env_pro.properties</value> </property> <property name="childLocations"> <list> <value>classpath:/properties/${STG}/persion.properties</value> <value>classpath:/properties/${STG}/cmdc.properties</value> </list> </property> </bean> </beans>
3.配置spring的主配置文件
代码:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="net.bill99"></context:component-scan> <!--引入配置文件--> <import resource="classpath:context/envLoad.xml"></import> <!--引入bean文件--> <import resource="classpath:spring/*.xml"></import> </beans>