Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot external application.properties

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. /etc/projectName/application.properties).

I tried several methods:

  1. @PropertySource("file:/etc/projectName/application.properties") as annotation at ApplicationConfig.java

  2. spring.config.location=/etc/projectName/application.properties in my application.properties in resources

I tested it with spring.port. The first method only added properties but didn't override them.

like image 514
greenhalos Avatar asked Jan 04 '15 22:01

greenhalos


People also ask

How do I configure externalize in spring boot?

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.

How do you externalize a constants from a spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.

How do you specify file path in application properties in spring boot?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.


3 Answers

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.

like image 63
JR Utily Avatar answered Oct 16 '22 13:10

JR Utily


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.

like image 3
Ramzi Guetiti Avatar answered Oct 16 '22 11:10

Ramzi Guetiti


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

like image 2
Ian Lim Avatar answered Oct 16 '22 13:10

Ian Lim