Read Spring Bean Values from Property File


This example uses a BeanFactoryPostProcessor called PropertyPlaceholderConfigurer to read spring.xml config values from a property file.

Example

people2.properties

jondoe.name=Jon Doe

janedoe.name=Jane Doe
janedoe.age=18
janedoe.email=jane.doe@gmail.com
janedoe.address=london

people2.xml

<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">

<!-- Person Beans -->
<bean id="jondoe" class="com.skills421.examples.spring.model.Person">
<property name="name" value="${jondoe.name}" />
</bean>

<bean id="janedoe" class="com.skills421.examples.spring.model.Person">
<property name="name" value="${janedoe.name}" />
<property name="age" value="${janedoe.age}" />
<property name="email" value="${janedoe.email}" />
<property name="address" ref="${janedoe.address}" />
</bean>

<!-- Address Beans -->
<bean id="london" class="com.skills421.examples.spring.model.Address">
<property name="addressLines">
<list>
<value>House of Commons</value>
<value>Parliament Square</value>
</list>
</property>
<property name="town" value="London" />
<property name="county" value="Greater London" />
<property name="postcode" value="SW1 1AA" />
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="people.properties" />
</bean>
</beans>

note that we use the PropertyPlaceholderConfigurer which is a BeanFactoryPostProcessor provided specifically for working with property configuration.

TestPeople2.java

package com.skills421.example.spring;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.skills421.examples.spring.model.Person;

public class TestPeople2
{
private static AbstractApplicationContext context;

@BeforeClass
public static void setupAppContext()
{
context = new ClassPathXmlApplicationContext("people2.xml");
}

@AfterClass
public static void closeAppContext()
{
if(context!=null)
{
context.close();
}
}

@After
public void printBlankLine()
{
System.out.println();
}

@Test
public void test1()
{
System.out.println("jondoe");
Person person = context.getBean("jondoe",Person.class);
System.out.println(person);
}

@Test
public void test2()
{
System.out.println("janedoe");
Person person = context.getBean("janedoe",Person.class);
System.out.println(person);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: