请问Java程序员在部署正式、测试数据时最麻烦的是什么??
当然是部署正式、测试环境时需要修改配置文件。
万一一个闪失,就等着背黑锅吧。尽管程序员一直都是背黑锅的主,财务对不上账了怪我们软件不行,门店销售额达不到目标怪我们服务器卡。
所以,我们如何杜绝这个闪失。
那在Spring3.0之后的版本,咱们就有招了。
我们可以使用 spring.profiles.active 来区分。
那做法的话就是下面两步:
一、在spring配置文件中配置两种环境信息(把我们的功能Bean 放在一个拥有profile属性里的Beans里)
<beans profile="dev">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configure-test.properties</value>
</list>
</property>
</bean>
</beans>
<beans profile="prd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configure.properties</value>
</list>
</property>
</bean>
</beans>
这个配置一定要单独放一个spring文件,然后在applicationContext.xml(或者其他的主入口文件)里import进来,如果你混淆做,就会报错:
发现了以元素 'bean' 开头的无效内容。应以 '{"http://www.springframework.org/schema/beans:beans}
二,环境配置用于容器区分用哪个profile,这个大家自己选
1)在web.xml中配置使用哪种环境信息,tomcat启动的时候自动去加载对应的环境信息
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
2) tomcat启动参数
tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件
JAVA_OPTS="-Xms1g -Xmx2g -Xss256k -Dspring.profiles.active=pre"
3) 如果是在myeclipse或其他ide里 我们也可以设置JVM的启动参数
5) 如果测试用例里使用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@ActiveProfiles("dev")
public class HelloJavaTest {
}