java jar -D option passing multiple external config files - spring-boot

I have two configuration files that have to be read using spring boot application, however, only the first configuration property file is resolved (external_application.properties) , later one (queries_config.properties) is not getting detected any issue in passing in the command line.
java -Dexternal.app.properties=file:external_application.properties -Dqueries.config.properties=file:queries_config.properties -jar testsnapshot.jar

If you are using spring boot so you can use
java
-Dspring.config.location=file:external_application.properties,file:queries_config.properties
source

I did like below which resolved over all problem..
java -Dspring.config.location=classpath:file:///C:\Users\configfiles\ -jar testsnapshot.jar
secondly to lookup for external config
#Component
#PropertySource(value = "${spring.config.location}queries_config-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class QueriesConfig {
}
#Component
#PropertySource(value = "${spring.config.location}external_application-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class ExternalConfig {
}

Related

Springboot Jboss system property issue

In our Springboot MVC project we need to get context path. The following code works fine if we use embedded tomcat. Otherwise if we deploy war file to jboss server, context path returns null.
I tried to add context-root property to jboss-web.xml but changed nothing. I also tried to add "server.servlet.contextPath" property to application.properties file but it doesn't work.
Spring boot version : 2.2.0.RELEASE
Code:
#Configuration
public class EurekaClientConfigBeanListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
String pathValue = System.getProperty("server.servlet.contextPath");
}

How can I load additional properties file in a spring boot application packaged as a war?

I have a standard springboot web app. I want to load properties file that's not in the classpath. application.properties is in the classpath and is being read correctly.
I don't have an issue when I'm building a jar. I just put the .properties alongside the jar and it works. But when I package a war, I couldn't get the application to read the properties file .
You put the properties file parallel to application.properties file and load it in a class like this:
#PropertySource("classpath:foo.properties")
public class My class {
#Value( "${some.property}" )
String myProp;
}
You can using ClassPathResource. given Class for loading resources.
This is sample code for you
ClassPathResource resource = new ClassPathResource("/application/context/blabla.yml");
InputStream inputStream = resource.getInputStream();
File file = resource.getFile();
// using inputStream or file
ClassPathResource
You can use spring application.properties to have spring profiles and use the spring profiles to define separate properties for each environment as you like.You can even separate out the spring profiles in to different files like appication-dev.properties etc so that you can have each spring profile in different files.
You can read the properties by using #Configuration annotation :
#Configuration
#EnableConfigurationProperties(TestProperties.class)
public class MySampleConfiguration {
}
Here TestProperties.class is used to map the values from the property file or yaml .
Reference in detail : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Load multiple external configuration file in spring boot

How to load multiple external configuration properties file in spring boot. Please find the below command to load external properties file when run the jar file.
"java -jar -Dspring.config.location= myBootProject.jar"
Like we add one or two configuration path, but when we add more than two configuration then how would we configure?
In spring you can do like below:
#Configuration
#PropertySource({
"classpath:app-config.properties",
"classpath:dtabase.properties"
})
public class AppConfig {
#Autowired
Environment env;
}
If you are using spring4 and Java8 or Greater:
#Configuration
#PropertySources({
#PropertySource("classpath:app-config.properties"),
#PropertySource("classpath:database.properties")
})
public class AppConfig {
//configuration classes
}
If a property key is duplicated, the last declared file will ‘win’ and override.
Read this for more information and complete samples
https://www.mkyong.com/spring/spring-propertysources-example/
Hope it helps!

Read both internal and external property file using Spring boot

I have a spring boot application jar
#SpringBootApplication
#EnableScheduling
#PropertySource({"classpath:1.properties","classpath:2.properties"})
public class MyClass {
//My class
}
1.properties is in src/main/resources
2.properties is in server location project location
| MyClass.jar
| config
| 2.properties
But I am getting file not found for 2.properties when starting the application.Please let me know what I could be missing here.
Like mentioned in the comment, your 2.properties file is not under your classpath. You can only use classpath if your file really exists in your jar or war.
To get the 2.properties you should use the command file: instead of classpath:.
#PropertySource("classpath:1.properties","file:${application_home}2.properties")
I am not quite sure if its still necessary to have an OS environment variable or a system property to set the path to your property file. In this case I named it application_home.

Change search location in Spring boot 1.3.0

I would like externalize the configuration of my aplication, I use Spring Boot 1.3.0, and I kwnow that the class ConfigFileApplicationListener has a default value DEFAULT_SEARCH_LOCATIONS. How can I change the source of my configuration, before this class load the default properties source?
You can use #PropertySource annotation on your configuration class (which is annotated #SpringBootApplication or #Configuration).
Like this:
#SpringBootApplication
#PropertySource(value = { "classpath:other.properties", "file:./other-external.properties" }, ignoreResourceNotFound = true)
public class MyApplication {
It will search first other.properties in your classpath and then looks the external file other-external.properties right outside of your jar or war file. Latest files overwrites values of other files (if they exists)
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details.

Resources