Spring boot external application.properties

前端 未结 5 668
长发绾君心
长发绾君心 2020-12-15 22:20

I am developing a spring boot application

I want to override some properties in src/main/resources/application.properties with an external file (e.g.

相关标签:
5条回答
  • 2020-12-15 22:45

    Note: The following solution will replace the old application.properties entirely

    You can place your application.properties in one of the following locations:

    • Under /config sub-directory of the current directory
    • The current directory
    • A classpath /config package
    • The classpath root

    And then exclude the default one under resources directory, like:

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/application.properties</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
    

    Find more details in this link

    0 讨论(0)
  • 2020-12-15 22:51

    If you do not want to use any solution from above, you could do the following in the start of your main(String args[]) method (before the usage of SpringApplication.run(MyClass.class, args):

    //Specifies a custom location for application.properties file.
    System.setProperty("spring.config.location", "target/config/application.properties");
    
    0 讨论(0)
  • 2020-12-15 22:53

    I had the same requirement like yours( war package instead of a fat jar) and I manged to externalize the configuration file: In my main class I made this configuration:

    @SpringBootApplication
    @PropertySource("file:D:\\Projets\\XXXXX\\Config\\application.properties")
    public class WyApplication extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(WyApplication.class, args);
        }
    
    }   
    

    Hope this will help you. Good luck.

    0 讨论(0)
  • 2020-12-15 22:55

    Notes

    1) Tomcat allows you to define context parameters 2) Spring Boot will read the properties defined in Tomcat context parameters as if you are defining them as -Dsomething=some_value

    Options

    Option 1: Hence a possible way is for you to define spring.config.location at the Tomcat context paramter:

    <Context>
        <Parameter name="spring.config.location" value="/path/to/application.properties" />
    </Context>
    

    Option 2: define a system property at the Tomcat's setenv.sh file

    Dspring.config.location=/path/to/application.properties
    

    Option 3: define an environment variable: SPRING_CONFIG_LOCATION

    0 讨论(0)
  • 2020-12-15 23:02

    I always use --spring.config.location= in the command line as specified in the documentation, and you can put various files in this, one with default values and another with the overridden ones.

    Edit:
    Alternatively, you could also use something like :

    @PropertySources({
        @PropertySource("classpath:default.properties")
        , @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true)
    })
    

    and specify a external.config in your application.properties.
    This would provide a default path for overridding config, that is still overriddable itself by specifying a --external.config in the command line.
    I use this with ${external.config} being defined as a system env variable, but it should work with a application.properties variable too.

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