Spring: Injecting different properties file according to profile

后端 未结 2 1121
盖世英雄少女心
盖世英雄少女心 2020-12-08 22:26

First, some context:

I\'m currently working on a project in which I use the Spring framework on Google\'s AppEngine (GAE) to fetch some data from one of Google\'s se

相关标签:
2条回答
  • 2020-12-08 23:08

    A couple of options:


    System Variables

    You can use a prefix to control environment specific properties, this can be done by using system variables:

     <util:properties id="googleProperties" 
                      location="WEB-INF/${ENV_SYSTEM:dev}/google.properties" />
    

    In this case it will always look under:

     <util:properties id="googleProperties" 
                      location="WEB-INF/dev/google.properties" />
    

    by default, unless a ENV_SYSTEM system variable is set. If it is set to qa, for example, it will automatically look under:

     <util:properties id="googleProperties" 
                      location="WEB-INF/qa/google.properties" />
    

    Spring Profiles

    Another approach is to make beans profile specific. For example:

    <beans profile="dev">
        <util:properties id="googleProperties" 
                         location="WEB-INF/google-dev.properties" />
    </beans>
    
    <beans profile="qa">
        <util:properties id="googleProperties" 
                         location="WEB-INF/google-qa.properties" />
    </beans>
    

    The appropriate googleProperties will loaded depending on a profile set. For example this will load WEB-INF/google-dev.properties:

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.getEnvironment().setActiveProfiles( "dev" );
    ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
    ctx.refresh();
    
    0 讨论(0)
  • 2020-12-08 23:18

    You are on the right track, in our application we have the same scenario and we use "profiles" to manage the properties. We use two configuration files one for Production and another one for testing with profiles set accordingly.

    0 讨论(0)
提交回复
热议问题