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.

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.

Spring Boot Features, Spring boot external application. properties. @PropertySource("file:/etc/projectName/application. properties") as annotation at ApplicationConfig. java. spring. config. location=/etc/projectName/application. properties in my application. properties in resources. Spring Boot allows you to externalize your configuration so 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. Property values can be injected directly into your beans using the @Valueannotation, accessed

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.

Spring boot external application.properties, If you have not changed the defaults of Spring Boot (meaning you are using @​EnableAutoConfiguration or @SpringBootApplication and have  Spring Boot binds external properties from application.properties (or .yml) (and other places) into an application at runtime. There is not (and technically cannot be) an exhaustive list of all supported properties in a single location because contributions can come from additional jar files on your classpath.

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

Spring Boot: Is it possible to use external application.properties files , Learn how to load Spring properties from outside the jar package. Ideally, we should keep it external to the packaging to be able to make changes properties file from a location outside the jar in a Spring Boot application. By default, Spring Boot look for externalized default property file application.properties into given below predetermined locations: -- In the classpath root. -- In the package "/config" in classpath. -- In the current directory.

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

Spring Properties File Outside jar, External application.property file – Going outside jar/war. What if you don't or even can't put environment properties inside the jar file? For  Defining Properties. Spring Boot application loads configuration properties from application.properties file located in the classpath by default. Open src/main/resources/application.properties file and add the following properties to it -.

Spring Boot application.properties file, Spring Boot is ignoring logging levels from external application packaged in war file on Tomcat. If yes declare a property spring.config.location=classpath: . Another approach is to create a file by name application.properties and package it with your jar. This file contains the key, value pairs for the properties. One can name these property files with any name, but the name has to be provided to the spring configuration property so that it can read that file.

spring.config.location tomcat, When you run your Spring Boot application, by default, property values configuration file (e.g. -Dconfig.location=/path/to/external.properties )  By convention, Spring Boot looks for an externalized configuration file – application. properties or application.yml – in 4 predetermined locations in the following order of precedence: A /config subdirectory of the current directory; The current directory; A classpath /config package; The classpath root

Externalized configuration in Spring Boot, Spring Boot makes it easy to create stand-alone, production-grade Spring based Spring Boot attempts to coerce the external application properties to the right  One handy feature of Spring Boot is externalized configuration and easy access to properties defined in properties files. An earlier article described various ways in which this can be done. We are now going to explore the @ConfigurationProperties annotation in greater detail.

Comments
  • Drop the application.properties in the same directory as the jar file, it will be picked up automatically.
  • I am using a war file, building with maven in Intellij and deploying into a Tomcat, so no jar-file
  • Add to your question as that influences the answer. Regardless you should use option 2 as the properties loaded by Spring Boot are ordered specifically, adding a @PropertySource will add your loaded properties to the end of the chain of PropertySources that are consulted.
  • greenhalos, did you ever find a resolution to this problem? I have the same issue with my war file. I'd rather not add arguments/parameters to the tomcat startup config, but I will if I have to.
  • @M.Deinum - actually, I found that application.properties needs to be in the current working directory to get picked up.
  • but i wanted to do it without specifying it in the command line but in application properties or ass annotations
  • I would really suggest not to do it that way : it means you are linking your source code with the external environment, and so that your source code would potentially not work anymore on another machine (not speaking of the root privilege needed to write your config file). IMHO, things that are specific of the external env should remain outside of the source.