By default, Spring Boot will look for your externalized properties in a file named application.properties located in one of the following folders :
- classpath root
- in the package /config in classpath
- in the current directory
- /config subdirectory of the current directory
You may want to use an externalized properties file in your Spring Boot application with a different name. This tutorial will show 3 different ways to rename application.properties in Spring boot.
Consider we want to rename application.properties to conf.properties
Table of contents
Command line arguments
The first way to rename application.properties in Spring boot is by using the command line argument -- spring.config.name={YOUR_FILE_NAME}. Below an example :
1 |
java -jar myproject.jar --spring.config.name=conf |
If you are using Eclipse, below the steps to follow :
- Right-Click on your Main class, Run As -> Run configurations…
- Go to Arguments tab
- And in Program arguments region past : --spring.config.name=conf
- Click Apply and then Run
Now Spring Boot will look for a file named conf.properties located in classpath root, in the package /config in classpath, in the current directory or in /config subdirectory of the current directory.
Environment variable
You can set the name of your externalized properties file in the SPRING_CONFIG_NAME environment variable as below :
1 2 |
set SPRING_CONFIG_NAME=conf java -jar spring-boot-rename-application-properties-0.0.1-SNAPSHOT.jar |
If you work with eclipse and you set the SPRING_CONFIG_NAME as a global system environment variable, you need to restart your IDE.
Programmatically
You can rename the Spring Boot properties file programmatically as below :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class) .properties("spring.config.name:conf") .build() .run(args); } } |