一般情况下,通过web.xml,spring profile的配置,搭配具体的配置文件,就可以实现配置文件的快速切换了,
但,有时候自己编写代码读配置文件,这个时候最好也根据环境读具体的配置
public class PropertiesConstant {
private static String profile = "" ;
static {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
profile = servletContext.getInitParameter("spring.profiles.active");
if(profile != null){
profile += "/";
}
else{
profile = "";
}
}
private static Properties props = PropertiesUtil.readProperty("/conf/resource/" + profile + "resource.properties");
public static final String SSO_DOMAIN_BASE_USRL = props.getProperty("SSO_DOMAIN_BASE_USRL");
web.xml
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>smart</param-value>
</context-param>
<context-param>
<param-name>spring.liveBeansView.mbeanDomain</param-name>
<param-value>development</param-value>
</context-param>
spring-profile配置
<description>spring profile配置</description>
<!-- 开发环境 -->
<beans profile="development">
<context:property-placeholder
location="classpath:conf/resource/development/*.properties" />
</beans>
<!-- 生产环境配置文件 -->
<beans profile="production">
<context:property-placeholder
location="classpath:conf/resource/production/*.properties" />
</beans>
<!-- 发布数据库 -->
<beans profile="release">
<context:property-placeholder
location="classpath:conf/resource/release/*.properties" />
</beans>