The Read Properties File in Spring Xml tutorial show you how to load properties from a file using spring property placeholder. In Spring, we can use PropertyPlaceholderConfigurer to parameterize property values from a bean definition in a separate file. This helps developer organize specific properties in a external file instead of having to hard code values in the application. PropertyPlaceholderConfigurer also support to load multi property files, this allows developer can divide and store property values into different file with different purposes.
Here, we have two property files and store in classpath.
application.properties file
1 2 |
name = Admin description = Administrator |
jdbc.properties
1 2 3 4 |
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/javabycode jdbc.username = javabycode jdbc.password = mypassword |
Create simple POJO
Here is a simple POJO where we are going to load the properties to .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.javabycode; public class User { private Long id; private String name; private String description; public void setId(Long id) { this.id = id; } public Long getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setDescription(String description) { this.description = description; } public String getDescription() { return description; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", description='" + description + '\'' + '}'; } } |
Read Properties File in Spring XML using Property Placeholder
To read properties from file application.properties and jdbc.properties we must configure spring xml file like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<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:application.properties,jdbc.properties"/> <bean class="com.javabycode.User"> <property name="name" value="${name}"/> <property name="description" value="${description}"/> </bean> <bean id="userDAO" class="com.javabycode.UserDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> </beans> |
Let’s dig deeper:
The
1 |
<context:property-placeholder location="classpath:application.properties,jdbc.properties"/> |
This configuration declares a bean and set the loaded properties through setter injection.
1 2 3 4 |
<bean class="com.javabycode.User"> <property name="name" value="${name}"/> <property name="description" value="${description}"/> </bean> |
The above configuration uses the expression language (el) notation ${property.name} and it is often called “key”. At runtime, the key is replaced with the corresponding value from the property file that is declared in Spring xml configuration file. If the key doesn’t exist, an exception is thrown at startup
Similary, we can inject value from jdbc properties for dataSource bean like below
1 2 3 4 5 6 7 |
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> |
Testing the read properties file
We create a main class to get a User bean that is assigned the loaded properties through setter injection. And save this bean in database using data source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApplication { public static void main(String... args){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); User user = context.getBean(User.class); UserDAO userDAO = context.getBean(UserDAO.class); userDAO.saveUser(user); } } |
Run the main above and see output like below
1 |
Created Record User Name = Admin Description = Administrator |
That’s all on the tutorial Read Properties File in Spring Xml using PropertyPlaceholderConfigurer