Jenny
Jenny

Reputation: 405

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

Upvotes: 1

Views: 16409

Answers (4)

Navin Gelot
Navin Gelot

Reputation: 1334

According to spring documentation:

Specific to you question you can use this solution : Application property files

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values, properties are considered in the the following order:

1.Command line arguments.

2.Java System properties (System.getProperties()).

3.OS environment variables.

4.@PropertySource annotations on your @Configuration classes.

5 Application properties outside of your packaged jar (application.properties including YAML and profile variants).

6.Application properties packaged inside your jar (application.properties including YAML and profile variants).

7.Default properties (specified using SpringApplication.setDefaultProperties).

For each configuration here is a detailed documentations from spring it-self : features-external-config

Upvotes: 1

Soniya Mohan
Soniya Mohan

Reputation: 4656

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

Upvotes: 2

jcompetence
jcompetence

Reputation: 8383

By using the annotation PropertySource

If file within classpath:

 @Configuration
 @PropertySource(value="classpath:org/springframework/context/annotation/p1.properties")
 public class Config {
 }

If file is external:

@PropertySource("file:/external/path/to/application.properties")
public class Config {
}

Upvotes: 2

Jacob
Jacob

Reputation: 452

Please refer to below link on all possible options while using external application.properties -

https://docs.spring.io/spring-boot/docs/2.5.6/reference/htmlsingle/#features.external-config.files

Upvotes: 1

Related Questions