Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing application.properties name in Spring Boot application

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?

like image 316
orrymr Avatar asked Feb 06 '17 09:02

orrymr


People also ask

How do I rename a spring boot application?

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.

Can we override application properties in spring boot?

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() ).

Where are application properties stored in spring boot?

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.

What is Spring Boot application properties?

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.

How do I change the value of a property in Spring Boot?

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.

How to have spring active profile in application properties?

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.

How to name a profile in Spring Boot application?

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;


3 Answers

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);
    }
}
like image 63
ccu Avatar answered Oct 12 '22 06:10

ccu


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
like image 39
Niranjan Kumar Avatar answered Oct 12 '22 05:10

Niranjan Kumar


You can set inline properties using SpringApplicationBuilder:

SpringApplicationBuilder()
  .properties("spring.config.name=myproject")
  .sources(MyApplication.class)
  .run(args);
like image 42
Abhijit Sarkar Avatar answered Oct 12 '22 06:10

Abhijit Sarkar