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.

6
  • 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: docs.spring.io/spring-boot/docs/current/reference/html/… Commented Oct 2, 2017 at 6:31
  • Yes the filename is apiapplication.properties Commented Oct 2, 2017 at 6:35
  • Have you tried with -D passed onto the JVM on startup? Commented Oct 2, 2017 at 6:55
  • I am deploying as a war file in statndalone tomcat.Not as a Fat Jar Commented Oct 2, 2017 at 6:55
  • Ok I am puzzled. Can you try to run the tomcat with remote debugging and just debug spring on startup? Commented Oct 2, 2017 at 6:56

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Not useful if the path properties file is selected at run time.
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.