Spring context:property-placeholder tutorial
last modified October 18, 2023
Spring context:property-placeholder tutorial shows how to use context:property-placeholder tag to externalize properties in a Spring application.
Spring is a popular Java application framework for creating enterprise applications.
Spring context:property-placeholder
The context:property-placeholder
tag is used to externalize properties in a separate
file. It automatically configures PropertyPlaceholderConfigurer
, which replaces
the ${}
placeholders, which are resolved against a specified properties file
(as a Spring resource location).
Spring context:property-placeholder example
The application uses context:property-placeholder
to configure
properties of a datasource.
pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ Application.java │ └───resources │ database.properties │ logback.xml │ my-beans.xml └───test └───java
This is the project structure.
<?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.zetcode</groupId> <artifactId>propertyplaceholder</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <spring-version>5.3.23</spring-version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <mainClass>com.zetcode.Application</mainClass> </configuration> </plugin> </plugins> </build> </project>
In the pom.xml
file, we have basic Spring dependencies spring-core
,
spring-context
, spring-jdbc
and logging logback-classic
dependency.
The exec-maven-plugin
is used for executing Spring application from the
Maven on the command line.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <logger name="org.springframework" level="ERROR"/> <logger name="com.zetcode" level="INFO"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration>
The logback.xml
is a configuration file for the Logback logging library.
db.url=jdbc:h2:mem:testdb db.username=testuser db.password=s$cret
These values are externalized in a database.properties
file. This approach
is more flexible than placing the values right into the XML file.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:database.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> </beans>
The context:property-placeholder
specifies the location of the properties
file; in our case, it is database.properties
file in any classpath directory.
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean>
A dataSource
bean is defined. It takes its values from the properties file via
the ${}
syntax.
package com.zetcode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.jdbc.datasource.SimpleDriverDataSource; public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { var ctx = new GenericXmlApplicationContext("my-beans.xml"); var dataSource = (SimpleDriverDataSource) ctx.getBean("dataSource"); logger.info("Url: {}", dataSource.getUrl()); logger.info("User name: {}", dataSource.getUsername()); logger.info("Password: {}", dataSource.getPassword()); ctx.close(); } }
This is the main application class. It retrieves the dataSource
bean
and prints its properties.
$ mvn -q exec:java 11:27:43.790 INFO com.zetcode.Application - Url: jdbc:h2:mem:testdb 11:27:43.790 INFO com.zetcode.Application - User name: testuser 11:27:43.790 INFO com.zetcode.Application - Password: s$cret
We run the application.
In this article we have shown how to use context:property-placeholder
to
externalize properties.
Author
List all Spring tutorials.