0

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.

Ricky
  • 2,662
  • 5
  • 25
  • 57
  • 1
    Is your file name is 'apiapplication.properties' then make sure you pass exactly that in 'spring.config.name'. Also... these properties need to be passed to JVM very early (according to the docs), try to run the JVM using -D flags and pass your configuration options there. As it can be read here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – Rafal G. Oct 02 '17 at 06:31
  • Yes the filename is apiapplication.properties – Ricky Oct 02 '17 at 06:35
  • Have you tried with -D passed onto the JVM on startup? – Rafal G. Oct 02 '17 at 06:55
  • I am deploying as a war file in statndalone tomcat.Not as a Fat Jar – Ricky Oct 02 '17 at 06:55
  • Ok I am puzzled. Can you try to run the tomcat with remote debugging and just debug spring on startup? – Rafal G. Oct 02 '17 at 06:56
  • I am not quite understanding what you are saying.Basically I package as a war file by maven and then deploy that war file to a standalone tomcat.So what happens is if application.properties is in the location it reads.Otherwise it does not deploy the war. – Ricky Oct 02 '17 at 06:59

3 Answers3

2

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

Pratik Ambani
  • 2,523
  • 1
  • 18
  • 28
1

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

-1

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;
      }
    }
Bobernac Alexandru
  • 169
  • 1
  • 2
  • 11