Properties placeholder in Spring Framework

on waitingforcode.com

Properties placeholder in Spring Framework

You're doing Java/C#/JavaScript and doing it great? But you're tired because of always facing the same problems. I was like that 4 years ago. I changed then to the data engineering field and it solved my existential problems :) If you want to follow my path, I prepared a course that will help you with that! Join the class!
Sometimes you will need to share properties values through multiple configuration files. They can, for example, have the same authentication information about database user, but, they can use different databases. In Spring we can resolve this problem with properties placeholders.

This article will show how to implement properties placeholders in Spring. The first part will be a short introduction to this functionality. We'll approach the use cases and describe the mechanism. Next part will present an implementation sample and the last one, some of "under the hood" explanations.

What is properties placeholder in Spring ?

Let's start by define a Spring's properties placeholder. Usually represented as an instance of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer class, the properties placeholder is an object able to retreive the values of defined properties directly in Spring's application context. It means that if you define a properties placeholder and you are looking for an properties variable, Spring will check if this variable doesn't exist in properties placeholder specified files. If it exists, its value will be returned. An variable looks like PHP's concatenated variables into Strings, ie: ${variableName}.

In which situations the properties placeholder can be helpful ? They are some reasons of theirs utility:
- in the case when we need to use some information in multiple configuration files or even in the classes. - we can use this separation when the developers can't modify configuration files structure but only theirs values
- when we use the same configuration's template (for example for datasource bean) but with different values according to environment (for example development environment, test environment and production environment)
- we want to add supplementary security layer by, for example, moving all properties files outside to web application repository

Implement properties placehoder in Spring

Through this part we'll try to prove some of utility announcements from the previous paragraph. So, we'll try to store properties file outside to web application context and use properties variables in multiple files (XML and Java classes). The first thing to do is to setup properties placeholder in Spring's context configuration file:


Next, we want to put placeholdedProps.properties file in defined path. The content if this file will contain database configuration:

db.user=root
db.url=jdbc:mysql://localhost/acl
db.password=
db.driverClass=com.mysql.jdbc.Driver
db.maxPoolSize=120

Now, we'll try to use these values in datasource configuration file:


The values will by taken directly from placeholdedProps.properties. The same behavior applies into Java's configuration files where we can reach the properties with @Value annotation:

@Value(value = "${db.maxPoolSize}")
private int maxPoolSize;

How does work properties placeholder in Spring ?

Now, let's see how does it work. The first concept to know is a property source, represented by the implementations of org.springframework.core.env.PropertySource abstract class. PropertySource exists to hold properties available from one source. Its method getSource() returns an object containing underlying source object like Properties or ServletContext instances.

Another important actor in properties placeholding process is property configurator, implemented for example as org.springframework.context.support.PropertySourcesPlaceholderConfigurer class. It's responsible for resolving given properties variables, like for example ${db.maxPoolSize}, to corresponding values. The values can be taken from defined files or directly from the system (according to configured system property mode: SYSTEM_PROPERTIES_MODE_NEVER, SYSTEM_PROPERTIES_MODE_FALLBACK or SYSTEM_PROPERTIES_MODE_OVERRIDE, set with setSystemPropertiesModeName() method).

The ${...} properties can be resolved thanks to processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props). It defines an implementation of org.springframework.util.StringValueResolver, used to resolve Strings.

In this article we discovered the properties placeholding in Spring framework. The first part was purely theoretical and presented properties placeholder idea. The next part treated about the implementation of properties placeholder. In the last one, we saw which classes were used to resolve properties in Spring and that the properties are encapsulated by PropertySource implementations.

Share on: