Spring: Injecting different properties file according to profile

后端 未结 2 1144
盖世英雄少女心
盖世英雄少女心 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:

     
    

    In this case it will always look under:

     
    

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

     
    

    Spring Profiles

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

    
        
    
    
    
        
    
    

    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();
    

提交回复
热议问题