spring 笔记3: Spring 多环境配置文件切换

使用Spring进行开发时,需要面对不同的运行环境,比如开发环境、测试环境、生产环境等。大多时候不同的环境需要不同的配置文件。网上很多资料都是使用Spring的Bean definition profiles 功能来实现(https://docs.spring.io/spring/docs/4.2.4.RELEASE/spring-framework-reference/html/beans.html#beans-definition-profiles)。这种方式就会出现同一个Bean要正对不同的环境分别声明,即繁琐,又造成相同的配置要重复很多遍。

比如:需要在vmoption 里面增加 -Dspring.profiles.active=dev

   

    <beans profile="dev">
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:properties/config_dev.properties</value>
                </list>
            </property>
        </bean>
 
    </beans>
    <beans profile="test">
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:properties/config_test.properties</value>
                </list>
            </property>
        </bean>
    
    </beans>
    <beans profile="uat">
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:properties/config_uat.properties</value>
                </list>
            </property>
        </bean>

    </beans>
    <beans profile="product">
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:properties/config_product.properties</value>
                </list>
            </property>
        </bean>
  
    </beans>

 

相比之下,我更喜欢这种方式:

需要vm option里面新增:-Denv=_rel。 如果没有新增属性,那么默认加载的是文件mybatis/jdbc.properties 。 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:mybatis/jdbc${env:}.properties</value>
                <value>classpath*:cache/radis${env:}.properties</value>
                <value>classpath*:applicationConfig${env:}.properties</value>
                <value>classpath*:rabbitmq/rabbitmq${env:}.properties</value>
                <value>classpath*:redisson/redisson${env:}.properties</value>
                <value>classpath*:webservice/webservice.properties</value>
                <value>classpath*:apiclient${env:}.properties</value>
                <value>classpath*:httprequest${env:}.properties</value>

            </list>
        </property>
    </bean>

仅就实现配置文件切换来说,两种方式中我倾向于后者,更简洁明了。当然前者有些功能是后者无法实现的,比如针对不同的环境有选择性的实例化对象。

 

转载于:https://www.cnblogs.com/fgq841103/p/9812169.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值