Ricky
Ricky

Reputation: 2750

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.

Upvotes: 0

Views: 5727

Answers (3)

Salvatore Iamundo
Salvatore Iamundo

Reputation: 61

It is not optimal to use @PropertySource as explained in https://stackoverflow.com/a/31027378/6503697 by Daniel Mora. Daniel gave a good solution but if you want to use spring.config.name property see my answare: https://stackoverflow.com/a/56445915/6503697

Upvotes: 1

Pratik Ambani
Pratik Ambani

Reputation: 2570

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

Upvotes: 2

Bobernac Alexandru
Bobernac Alexandru

Reputation: 209

I suggest you use the @PropertySource annotation and autowire an Enviroment object. Check the example:

    @PropertySource("file:/home/config_directory/")
    public class testClass{
        @Autowired
        private Environment environment;
        public DataSource dataSource(){

          BasicDataSource basicDataSource = new BasicDataSource();   
          basicDataSource.setDriverClassName(environment
                             .getProperty("database_manager.db.driver"));
          return basicDataSource;
      }
    }

Upvotes: -1

Related Questions