Load Properties from a file with Spring Property Placeholder
In this example we will show you how to load properties from a file using spring property placeholder. With PropertyPlaceholderConfigurer
you can externalize property values from a bean definition in a separate file. This allows the developer to load environment/deployment specific properties from a file without having to hard code values in the application. Lets walk through the steps.
Example application.properties file
Here are some properties that we want to load into a bean or simple pojo.
course.name = Learning Spring
course.description = Spring property placeholder
course.price = 0
Simple POJO
This is a simple POJO where we are going to load the properties to.
package com.memorynotfound;
public class Course {
private String name;
private String description;
private String price;
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "Course{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", price='" + price + '\'' +
'}';
}
}
Load Properties with Spring Property Placeholder
- We can load the property file using the
<context:property-placeholder>
element. This element takes alocation
attribute which points to the properties file that we want to load. In our case we are looking for a property file that resides at the classpath and has a name of application.properties. - Next we create a component where we are setting the loaded properties through setter injection. We can then set the properties using the expression language (el) notation
${property.name}
. The${property.name}
is replaced at runtime with the corresponding value from the property file. The key must exist in the property file otherwise this will throw an exception at startup indicating that the property could not be found.
<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"/>
<bean class="com.memorynotfound.Course">
<property name="name" value="${course.name}"/>
<property name="description" value="${course.description}"/>
<property name="price" value="${course.price}"/>
</bean>
</beans>
Bootstrap Spring and test properties
Lets test if the properties could be loaded.
package com.memorynotfound;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String... args){
ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
Course course = context.getBean(Course.class);
System.out.println(course);
}
}
Output
Course{name='Learning Spring', description='Spring property placeholder', price='0'}