Documente Academic
Documente Profesional
Documente Cultură
com/load-properties-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.
Simple POJO
This is a simple POJO where we are going to load the properties to.
Note: for this example we only included setter methods because we are setting the
properties in the app-config.xml file using setter injection.
package com.memorynotfound;
@Override
public String toString() {
return "Course{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", price='" + price + '\'' +
'}';
}
}
<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>
package com.memorynotfound;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Output
Course{name='Learning Spring', description='Spring property placeholder',
price='0'}
References
Property Placeholder Documentation