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:
package com.eMcREY.ChatServiceProject;
import org.springframework.beans.factory.annotation.Value;
@org.springframework.context.annotation.Configuration
public class Config {
// private @Value("${project.testVar:defaultValue}") String test;
// public String getTest() {
// return test;
// }
//
// public void setTest(String test) {
// this.test = test;
// }
// Openfire
private @Value("${openfire.customerKey}") String customerKey;
private @Value("${openfire.customerSecret}") String customerSecret;
private @Value("${openfire.host}") String host;
private @Value("${openfire.port}") int port;
private @Value("${openfire.clientPort}") int clientKey;
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;
}
}
pom: i have included this in pom
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</configuration>
my question is how to make this config
file read the application.properties
from another location outside the project.
Thank you
CodePudding user response:
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"));
}
}
CodePudding user response:
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
CodePudding user response:
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 {
}
CodePudding user response:
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