How to rename application.properties file in Spring Boot application?
It is a standard practice that during our production deployments, our application loads property files
from external locations. This helps us to change our configurations without changing our code. Sometimes we also change
default file names according to our needs in production environment. In this page, we will come to
know how to load external property file which is renamed into 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.
-- In the "/config" directory of current folder.
Now lets say, you have renamed your default application.properties file like myapp.properties.
There are 3 ways of loading it to your Spring Boot application.
Command line arguments
Use command line argument "--spring.config.name" to pass the renamed file name to the Spring Boot application as shown below:
Terminal |
java -jar myAppBuild.jar --spring.config.name=myapp
|
Environment variables
In the second approach, you can configure your externalized configuration details into environment variables
and your Spring Boot application will read it from your environment as shown below:
Terminal |
set SPRING_CONFIG_NAME=myapp
java -jar myAppBuild.jar
|
Programatically loding configurations
SpringBootWebApplication |
package com.java2novice.springboot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringBootWebApplication {
private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootWebApplication.class)
.properties("spring.config.name:myapp")
.build()
.run(args);
}
}
|
|