SEARCH BY

All

  • All
  • Java8
  • Spring

Property expansion in spring boot application with maven

Post a Comment

Introduction

properties files contains properties, which are useful to setup external configuration in the spring boot application. Majority of cases values for properties can be referenced with static values only, for example consider following sample properties file content
msg.welcome= ${wmsg}
msg.location=India
In the above properties file there are two properties one is welcomeand another one is location prefixed with msg, you can notice that location property is assigned with static value ( that means we have assigned value here directly ), but for welcome there is a placed holder ${...} with wmsg as a filled parameter, wmsg value will be extracted from environment variables or from .properties file or from java system variables.
In resource files, we can use ${...} placeholder as variables which are replaced by system or maven properties during build timeSpring Boot - Property expansion using Maven Resource Filtering

How to pass values to placedholder 

In the above example wmsg is a property palcedholder, where maven will try to find out values from environment variables or java system variables or from .properties file which are in class path.
  • By environment variable
  • By Java system properties
  • from .properties file
  • from pom.xml file ( but reads only predefined maven properties only )

How to create an environment variable in eclipse 

Follow the steps as shown in the bellow figure
creating environment variables in eclipse
creating environment variables in eclipse
By using @ConfigurationProperties we are mapping the .properties with POJO class. For detailed explanation about how to use @ConfigurationProperties go to this article.
@ConfigurationProperties(prefix = "msg")
public class MessageConfig {

 String welcome;
 
 @Value("${msg.location}")
 String location1;

 public String getWelcome() {
  return welcome;
 }

 public void setWelcome(String weclome) {
  this.welcome = weclome;
 }

 public String getLocation() {
  return location1;
 }

 public void setLocation(String location) {
  this.location1 = location;
 }

}

RestController Endpoint


Here i have created following RestController to access proprieties into my output
@RestController
@RequestMapping("/message")
public class MessageController {

 @Autowired
 MessageConfig messageConfig;
 
 @GetMapping(value  ="/m1")
 public String welcomeMessage()
 {
  return messageConfig.getWelcome()+" at " + messageConfig.getLocation();
  
 }
 
}

Output with Environment variable based property placeholder value



Following diagram shows you deep understanding how mapping and placeholder will work with maven

Environment based property value setup
Environment based property value setup

Using java system properties to set value for placeholder

Using java system variable is one of the way to assign value for property placeholder element, we are using eclipse run configurations window to set java system variable. -D is prefixed with system variable name to assign the value, for example -Dwmsg="value for wmsg here" is the syntax to set system property value. As i said iam setting environment variable in eclipse as shown in the following figure.
java system properties in eclipse
java system properties in eclipse

output with java system variable setup
In the following screen you can notice that value fro wmsg is taking from java system variable ( as shown from the above figure )
java system properties values placeholder
java system properties values placeholder

Setting value for placeholder in application.properties file

This is another way to setup value for property placeholder, we can set property placeholder as another property element in .properties file as shown in the following example.
wmsg= message from properties file
msg.welcome= ${wmsg}
msg.location=India
Output
placeholder value setup from .properties file
placeholder value setup from .properties file

Hierarchy of finding value for placeholder

Spring boot with maven will try to find value for placeholder first from java system properties , then try to find from environment variables then try to find in .properties file as element
Placeholder value reference finding hierarchy
Placeholder value reference finding hierarchy 

Read from pom.xml file properties

We can set the value for ${...} placed holder from properties of pom.xml file,remember we can access or set only predefined properties of maven. For example to access java version using in the application we can use ${java.version} as a placeholder in properties file.
msg.welcome=we are using java version ${java.version}

Using palceholder property in pom.xml file

Now we will see how to use placehoder proeprty ${...} with in pom.xml file, we can apply same in any xml based bean configuration procedure too.
<properties>
 <demoName>${dname}</demoName>
</properties>
In the above code we have created a property named as demoName which is a user defined property assigned with a placeholder dname with ${...}, we can set the value for dname with any one of the above procedures mentioned.
Recall once again demoName is a user defined property, and ${...} is allowing to set the value for placeholder from java system properties or environment variables or from any .properties file which are added to class path or from any one of the predefined maven propertied from pom.xml file. So ${...} is not enough to access or set value from demoName, then here we need to expand properties with in spring boot application.

Property expansion with in spring boot application by using maven

Now this is the time as to deal with user defined properties in maven properties bu using spring boot application. In property expansion to avoid confusion between maven placeholder and spring boor place holder we need to use @...@ in place of ${...}. @...@ is useful to access user defined properties from outside of the pom.xml file that means in any .properties file generally.
To use @...@ to expand property no need to set any configurations, but just we need to add the following spring boot parent starter plugin in pom.xml file.
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>
Once the above plugin added into pom.xml file the our application is ready to use @...@ to property expansion.
Following is the example of using @...@ as a placed holder in properties file
name= Name from properties file
dname=This is a demo name from properties into pom file
wmsg= message from properties file
msg.welcome=we are using java version ${java.version} + @demoName@
msg.location=India
In the above you can see dname value has been created and demoName is using with @...@ placeholder to access user defined property from pom.xml file

References

Further Reading

jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox