Load properties file in Spring depending on profile

前端 未结 2 569
一生所求
一生所求 2020-12-17 04:25

I have a Spring 3.1 application. Let\'s say it has an XML with the following content:



        
相关标签:
2条回答
  • 2020-12-17 04:37

    You can do:

      <context:property-placeholder location="classpath:${spring.profiles.active}.properties" />
    

    It works fine, but is perhaps not adapted when using multiple profiles in the same time.


    When declaring 2 property placeholders, if the 1st one does not contain all the applications keys, you should put the attribute ignoring unresolvable = true, so that the 2nd placeholder can be used. I'm not sure if it is what you want to do, it may if you want both xx1 and xx2 profiles be active in the same time.

    Note that declaring 2 propertyplaceholders like that make them independant, and in the declaration of xx2.properties, you can't reuse the values of xx1.properties.


    If you need something more advanced, you can register your PropertySources on application startup.

    web.xml

      <context-param>
        <param-name>contextInitializerClasses</param-name>
        <param-value>com.xxx.core.spring.properties.PropertySourcesApplicationContextInitializer</param-value>
      </context-param>
    

    file you create:

    public class PropertySourcesApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    
      private static final Logger LOGGER = LoggerFactory.getLogger(PropertySourcesApplicationContextInitializer.class);
    
      @Override
      public void initialize(ConfigurableApplicationContext applicationContext) {
        LOGGER.info("Adding some additional property sources");
        String profile = System.getProperty("spring.profiles.active");
        // ... Add property sources according to selected spring profile 
        // (note there already are some property sources registered, system properties etc)
        applicationContext.getEnvironment().getPropertySources().addLast(myPropertySource);
      }
    
    }
    

    Once you've done it you just need to add in your context:

    <context:property-placeholder/>
    

    Imho it's the best way to deal with spring properties, because you do not declare local properties everywhere anymore, you have a programmatic control of what is happening, and property source xx1 values can be used in xx2.properties.


    At work we are using it and it works nicely. We register 3 additional property sources: - Infrastructure: provided by Puppet - Profile: a different property loaded according to the profile. - Common: contains values as default, when all profiles share the same value etc...

    0 讨论(0)
  • 2020-12-17 04:38

    I have decided to submit and answer to this as it has not yet been accepted. It may not be what you are looking for specifically but it works for me. Also note that i am using the new annotation driven configuration however it can be ported to the xml config.

    I have a properties file for each environment(dev.properties, test.properties etc)

    I then have a RootConfig class that is the class that is used for all the configuration. All that this class has in it is two annotations: @Configuration and @ComponentScan(basePackageClasses=RootConfig.class). This tells it to scan for anything in the same package as it.

    There is then a Configuration Containing all my normal configuration sitting wherever. There is also a configuration for each environment in the same package as the root configuration class above.

    The environment specific configurations are simply marker classes that have the following annotations to point it to the environment specific properties files:

    @Configuration
    @PropertySource("classpath:dev.properties")
    @Import(NormalConfig.class)
    @Profile("dev")
    

    The import tells it to bring in the normal config class. But when it gets in there it will have the environment specific properties set.

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