Spring PropertyPlaceholderConfigurer Example
March 12, 2023
On this page, we will provide Spring PropertyPlaceholderConfigurer
example using annotation and XML to externalize property values. In case of XML configuration, simply create bean for PropertyPlaceholderConfigurer
and set the location of property file to externalize. In case of annotation, we need to create static bean of PropertyPlaceholderConfigurer
in Java configuration.
The
PropertyPlaceholderConfigurer
is used to resolve ${...}
placeholders against a property. It can be local properties or system properties or environment variables. We can use PropertyPlaceholderConfigurer
using XML as well as annotation.
The
PropertyPlaceholderConfigurer
externalizes the property configuration. Properties should be configured in a property file inside or outside application. So that changing properties frequently will not cause to change the code. Here on this page we will provide complete example for PropertyPlaceholderConfigurer
using XML as well annotation.
PropertyPlaceholderConfigurer using XML
For thePropertyPlaceholderConfigurer
demo in XML , we are using an application which will interact with database. We will externalize our JDBC connection properties in a property file.
XML for PropertyPlaceholderConfigurer
Find the application context XML file.app-conf.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties" /> </bean> <bean id="dtSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="villageDAO" class="com.concretepage.VillageDAO"> <constructor-arg name="dataSource" ref="dtSource"/> </bean> </beans>
Create Table in MySQL
Create a table in MySQL in which we will insert the data and then select it.Table: village
CREATE TABLE `village` ( `name` VARCHAR(50) NULL DEFAULT NULL, `district` VARCHAR(50) NULL DEFAULT NULL ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB;
Create JDBC Property File to Externalize Property Values
Create a JDBC property file where we will configure our JDBC connection properties.jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/concretepage jdbc.username=root jdbc.password=
Create DAO
Find a DAO class used in our demo.VillageDAO.java
package com.concretepage; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import javax.sql.DataSource; public class VillageDAO { private DataSource dataSource; public VillageDAO(DataSource dataSource){ this.dataSource= dataSource; } public void save() throws SQLException{ Connection con=dataSource.getConnection(); Statement st= con.createStatement(); st.executeUpdate("insert into village values ('dhananjaypur','varanasi')"); } public Map<String, String> selectFirstRow() throws SQLException { Connection con=dataSource.getConnection(); Statement st= con.createStatement(); ResultSet rs = st.executeQuery("select name, district from village"); rs.first(); Map<String, String> map = new HashMap<>(); map.put("name", rs.getString(1)); map.put("district", rs.getString(2)); System.out.println(map); return map; } }
Run Demo
Run the demo.SpringDemo.java
package com.concretepage; import java.sql.SQLException; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String[] args) throws SQLException { AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml"); VillageDAO vill=(VillageDAO)context.getBean("villageDAO"); vill.save(); vill.selectFirstRow(); context.registerShutdownHook(); } }
{district=varanasi, name=dhananjaypur}
Gradle File
Find the gradle file to resolve the JAR dependencies.build.gradle
dependencies { compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE' compile 'commons-dbcp:commons-dbcp:1.4' compile 'mysql:mysql-connector-java:5.1.31' }
PropertyPlaceholderConfigurer using Annotation
To usePropertyPlaceholderConfigurer
using annotation, we will create a static bean of it in java configuration class. To externalize the properties we will use PropertyPlaceholderConfigurer
with @PropertySource
and @Value
.
@PropertySource: It loads property file.
@Value: It is used for expression-driven dependency injection.
Java Configuration: Create Static Bean for PropertyPlaceholderConfigurer
We will create a static bean ofPropertyPlaceholderConfigurer
in java configuration. The reason to create @Bean
method as static is that it must be instantiated very early in the spring container life-cycle. And hence PropertyPlaceholderConfigurer
bean will interfere with processing of @Value
annotation.
AppConfig.java
package com.concretepage; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @PropertySource("classpath:jdbc.properties") public class AppConfig { @Value("${jdbc.driverClassName}") private String driverClassName; @Value("${jdbc.url}") private String jdbcURL; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource getDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(jdbcURL); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } @Bean public VillageDAO villageDAO(DataSource dataSource) { return new VillageDAO(dataSource); } @Bean public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
Run Demo
Run the demo.SpringDemo.java
package com.concretepage; import java.sql.SQLException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringDemo { public static void main(String[] args) throws SQLException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); VillageDAO villageDAO = ctx.getBean(VillageDAO.class); villageDAO.save(); villageDAO.selectFirstRow(); ctx.registerShutdownHook(); } }
{district=varanasi, name=dhananjaypur}