8

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?

5 Answers 5

14

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that if your config file is in another folder you can set the location as well: System.setProperty("spring.config.location", "../config/");
2
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

Comments

2

You can set inline properties using SpringApplicationBuilder:

SpringApplicationBuilder()
  .properties("spring.config.name=myproject")
  .sources(MyApplication.class)
  .run(args);

2 Comments

it's missing new in new SpringApplicationBuilder()
Didn't worked for me. I end up using @PropertySource
1

Well, I'm not sure if this is the best approach (if a Spring guru sees this, please let me know if it is :), but I did the following:

@PropertySource(value={"classpath:my-project.properties"}, ignoreResourceNotFound = true)

3 Comments

This doesn't change the name of the application.properties, it adds a new property source to the environment. You ask a misleading question, then post a different answer and accept it, it's like a monkey scratching its own back :)
Fair point - I've changed the accepted answer :)
@AbhijitSarkar this is the perfect solution I guess because spring.config.name didn't worked for me 😒😒
0

You can also provide the following System properties (or environment variables) to change the behavior:

  • spring.config.name (SPRING_CONFIG_NAME): Defaults to an application as the root of the file name.
  • spring.config.location (SPRING_CONFIG_LOCATION): The file to load (such as a classpath resource or a URL). A separate Environment property source is set up for this document and it can be overridden by system properties, environment variables, or the command line.
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.