Load properties file in Spring depending on profile

前端 未结 2 568
一生所求
一生所求 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: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.

提交回复
热议问题