在Java类中取web.xml中配置的profile

一般情况下,通过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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值