1. Overview

Spring boot has made application configuration easy and fast. we can write application configuration in application.properties or yml file. But sometimes due to certain reasons, we need to change the name of application.properties, so here are different ways for the Spring boot change application properties name.

Spring boot provides multiple ways to change the name of application.properties file, so based on our requirement or suite to our logic we can use any one way of them.

Make sure that file is available at a proper location with given name. In our bellow examples, we have used my-config.properties instead of application.properties. so my-config.properties must be available at one of below locations:

  1. file:./config/    – my-config.properties should be at inside config director from where your .jar is available
  2. file:./                    – my-config.properties should be at the same location where .jar is available
  3. classpath:/config/    – In CLASSPATH  config dir
  4. classpath:/                   – In the root of CLASSPATH

2. 3 ways to change application.properties file name

2.1 Change properties file name using Command Line

Spring boot provides command line configuration called spring.config.name using that we can change the name of  application.properties.

Here properties file name will be my-config.properties which should be available proper location, guild line for properties file location is defined here,

–spring.config.name=my-config.properties is wrong configuration no need to pass extenstion of file.

> java -jar spring-boot-example-1.0-SNAPSHOT.jar --spring.config.name=my-config

2.2 Change properties file name using the environment variable

Most of the operating systems does not allow . (dot) in the environment variable, so we are going to use SPRING_CONFIG_NAME as environment variable name.

> set SPRING_CONFIG_NAME=my-config
> java -jar spring-boot-example-1.0-SNAPSHOT.jar

2.3 Set properties file name at runtime or programmatically

Sometimes we need to set configuration name at runtime or programmatically, so here we can use SpringApplicationBuilder which contains properties method where we can pass Array of properties. Properties syntax will be: PROPERTIES_NAME:VALUE

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by JavaDeveloperZone on 04-08-2018.
 */

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(SpringBootConfig.class)
                .properties("spring.config.name:my-config")
                .build()
                .run(args);
    }
}

4. Conclusion

In this article, We have learned how to change application.properties file name in spring boot application. We can set property name spring.config.name from the command line or runtime or SPRING_CONFIG_NAME as an OS environment variable.

5. References

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

2 comments. Leave new

Are people using this in conjunction with spring active profiles, configuration server, etc? If so, why/how?

Can you please tell me in details? We are not getting your view here.

Leave a Reply

Your email address will not be published. Required fields are marked *