Search code examples
springspring-bootapplication.properties

How to change external application.properties file name?


I am using spring boot and deploying it as a war in standalone tomcat.The below is my application class.

public class APIApplication extends SpringBootServletInitializer  {
    public static void main(String[] args) {

        configureApplication(new SpringApplicationBuilder()).run(args);
    }

    public static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
        return builder.sources(APIApplication .class).properties(getProperties());
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(APIApplication .class);
    }

    public static Properties getProperties() {
        Properties props = new Properties();
         props.setProperty("spring.config.location",
         "/home/config_directory/");
        props.setProperty("spring.config.name", "apiapplication");
        return props;

    }


}

But this does not work and does not read from the /home/config_directory/apiapplication.properties

Any help is appreciated.

EDIT

Also Tried

public static void main(String[] args) {
        System.setProperty("spring.config.location","/home/config_directory/");
        System.setProperty("spring.config.name", "apiapplication.properties");

        SpringApplication.run(DriverguidanceapiApplication.class, args);
        //configureApplication(new SpringApplicationBuilder()).run(args);
    }

Did not work too.


Solution

  • No need of manual configuration buddy!! Spring is here to help you with @PropertySource annotation.

    I'll share my code snippet where I've used what you are looking for.

    package XXXX;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    
    /**
     * Created by Pratik Ambani.
     */
    @Configuration
    @PropertySource(value = {"classpath:default.properties", "classpath:application.properties"}, ignoreResourceNotFound = true, name = "myServerConfigs")
    public class PropertySourceExample {
    
        @Value("${dev.baseURL}")
        private String localUrl;
    
        @Value("${sit.baseURL}")
        private String serverUrl;
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer xxxpropertyConfig() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
        @Bean
        protected String database() {
            Resource resource = new Resource();
            resource.setUrl(restAPIUrl);
            return resource;
        }
    }
    

    But wait, what are these dev.baseUrl and sit.baseUrl values and where are they coming from? Have a look at my properties files.

    application.properties

    dev.baseURL=http://localhost:7012
    

    default.properties

    sit.baseURL=http://myserver:7012
    

    Voilla!!! I'm able to read values from multiple files. Happy Coding. May code bless you. :)