orrymr
orrymr

Reputation: 2493

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?

Upvotes: 8

Views: 37247

Answers (5)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

You can set inline properties using SpringApplicationBuilder:

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

Upvotes: 2

orbita
orbita

Reputation: 179

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

Upvotes: 0

ccu
ccu

Reputation: 492

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);
    }
}

Upvotes: 13

orrymr
orrymr

Reputation: 2493

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)

Upvotes: 1

Niranjan Kumar
Niranjan Kumar

Reputation: 21

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

Upvotes: 2

Related Questions