简体   繁体   中英

Spring boot - Default properties file not being read when using profile

I have a spring boot 2 application with 2 property files:

  • application.properties
  • application-dev.properties

When I run the application in IntellijIdea using the dev profile, Spring reads both the profile specific file and the default file.

Once I build an executable jar file, I created a new application-prod.properties outside of the jar, with production environment properties and execute the application with --spring.profiles.active=prod expecting it to read this file AND the default application.properties , but it is only reading the file outside the jar (application-prod.properties).

Should Spring Boot read both files or am I expecting something that it does not do by default?

If it should, what can I be missing?

As mentioned by Michael, Spring boot supports property hierarchies .

I tried this and it works as expected, you can find a reference project in here

A few things that I can think about:

  • Extract the contents of your jar file and confirm that the file named application.properties is bundled in there.

  • You may be overwriting the spring.config.location property and using filenames instead of directories

From Spring Boot documentation

If you have specified any files in spring.config.location, profile-specific variants of those files are not considered. Use directories in spring.config.location if you want to also use profile-specific properties.

  • If you override the default spring.config.location then the application may not be able to find the default application.properties file.

By default, application.properties will be used to run the Spring Boot application. While running the JAR file, you need to specify the spring active profile. only one file will be read

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM