I've got my application.properties
file in /resources. I just want to change the name to <my-project-name>.properties
. According to this reference, I should be able to change the name by specifying spring.config.name as an environment property:
java -jar myproject.jar --spring.config.name=myproject
But is there a way I can do this with an annotation or within my codebase somehow?
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.
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values, properties are considered in the the following order: Command line arguments. Java System properties ( System. getProperties() ).
You will need to add the application. properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.
How Spring boot application.properties works? As of now we already know that the application.properties file contains the property for the application which helps the application to run in the different environment if we have any.
Create a spring boot project and create an endpoint which will read a property from the application.properties file and will return the value in the application.properties file. We will change the value in the application.properties file and we will expect the new value without restarting the server.
Let us understand how to have Spring active profile in application.properties. By default, application. properties will be used to run the Spring Boot application. While running the JAR file, we need to specify the spring active profile based on each properties file. By default, Spring Boot application uses the application.properties file.
As you can see you can create the application file like above, in the profile part you can mention the name of the profile you want. 2) Inside the application file we can give the name for the profile by using the key and properties defined by the spring boot application. Below see the syntax how we can do this in our application see below;
What I believe to be the most simple way to do this is to set a system property in the main method of your Spring Boot application entry point:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
// Tell Boot to look for my-project.properties instead of application.properties
System.setProperty("spring.config.name", "my-project");
SpringApplication.run(MyApplication.class, args);
}
}
spring.config.location - classpath:{myproject}.properties
It's worked for me.
And make sure the same classpath is be placed in the value of PropertySource if it(@PropertySource) exits in whereever in the app.
.
├src
| └main
| └resources
| └myproject.properties
You can set inline properties using SpringApplicationBuilder
:
SpringApplicationBuilder()
.properties("spring.config.name=myproject")
.sources(MyApplication.class)
.run(args);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With