Search code examples
javaspring-bootconfigurationapplication.properties

How to read the application.properties file from a location outside the spring boot application


I have a spring boot application from which I want to exclude the application.properties created by default in spring boot and read it from another location. However I noticed that the configuration file shown below, will always try to fetch for the application.properties file inside the project.

Config.java file:


public class Config {
    }
    public int getClientKey() {
        return clientKey;
    }
    public void setClientKey(int clientKey) {
        this.clientKey = clientKey;
    }
    public String getCustomerKey() {
        return customerKey;
    }
    public void setCustomerKey(String customerKey) {
        this.customerKey = customerKey;
    }
    public String getCustomerSecret() {
        return customerSecret;
    }
    public void setCustomerSecret(String customerSecret) {
        this.customerSecret = customerSecret;
    }
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    
    
    
    

}

my question is how to make this ```config``` file read the ```application.properties``` from another location outside the project.
Thank you

Solution

  • lets say, your application requires externalized properties like application.properties and another property file myapp.properties. The both properties can be in the same folder or they can be in different folder. There are 3 ways of doing it.

    Command line arguments

    In the first approach, all you need to do is pass folder names and property names as part of command line arguments as shown below:

    java -jar myapp.jar --spring.config.name=application,myapp
    --spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config
    

    Environment variables

    In the second approach, you can configure your externalized configuration details into environment variables and your Spring Boot application will read it from your environment as shown below:

    set SPRING_CONFIG_NAME=application,myapp
     
    set SPRING_CONFIG_LOCATION=classpath:/data/myapp/config,classpath:/data/myapp/external/config
     
    java -jar myapp.jar
    

    Programatically loding configurations

    package com.java2novice.springboot;
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.env.ConfigurableEnvironment;
     
    @SpringBootApplication
    public class SpringBootWebApplication {
     
        private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);
     
        public static void main(String[] args) throws Exception {
     
            ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootWebApplication.class)
                    .properties("spring.config.name:application,myapp",
                            "spring.config.location:classpath:/data/myapp/config,classpath:/data/myapp/external/config")
                    .build().run(args);
     
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
     
            logger.info(environment.getProperty("cmdb.resource-url"));
        }
    }