My question seems pretty simple but I could not find a way to implement it.
Consider the following case:
prop1 = value1
prop2 = value2
prop3 = value3
prop4 = value2 (Value same as prop2)
prop5 = value3 (Value same as prop3)
How to reuse value 2 and 3 (these are actually database specific properties) as I want the user to provide it only once and not repeat it.
Thanks.
Case 1:
I would elaborate my case as below:
I have two properties file - application.properties
and quartz.properties
.
application.properties:
prop1 = value1
prop2 = value2
prop3 = value3
quartz.properties
prop4 = value2 (Value same as prop2)
prop5 = value3 (Value same as prop3)
Please note: I cannot merge the contents of the two properties files and they are put separately for a reason.
In normal scenario I expected ${} to work but it doesn't work when the properties are in two different files I guess.
Case 2: [Simple scenario] I tried using just one application.properties. But even then I am not able to reuse the property values in same property file.
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>abc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xyz</name>
<description>xyz</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Quartz dependencies start -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<!-- Includes spring's support classes for quartz -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- Quartz dependencies End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
To change properties in a file during runtime, we should place that file somewhere outside the jar. Then we tell Spring where it is with the command-line parameter –spring. config. location=file://{path to file}.
So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.
Spring Boot - Using ${} placeholders in Property Files Spring also provides it's own variable substitution in property files. We just need to use ${someProp} in property file and start the application having 'someProp' in system properties or as main class (or jar) argument '--someProp=theValue'.
You can use placeholder for it like this:
prop1 = value1
prop2 = value2
prop3 = value3
prop4 = ${prop2} (Value same as prop2)
prop5 = ${prop3} (Value same as prop3)
But note, that this should be in the application.properties
so spring is aware of it. In case you want your custom file with properties it would make sense to config PropertyPlaceholderConfigurer
, as shown below:
@Configuration
public class PropertyPlaceholderConfigurerConfig {
@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("custom.properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
Now you should be abe to:
@Value("${prop4}")
private String someValue;
And it should inject you the right value in your bean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With