How to inject properties file data to bean in Spring


This tutorial will help you to inject the properties file data to the bean or class. The properties file stores the string type data in the form key and value pair similar to the Map. To load the properties file into IOC context scope Spring has given a class PropertyPlaceholderConfigurer which have a method setLocation(Resource location) to obtain the location of properties file.

Here we read the properties file data and after reading the data inject it into the required bean or class and by using the expression we can read the IOC context data.

1- Load properties file data into IOC context scope.

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

2- Inject context scope data into required bean/class.

<bean id="connect" class="org.websparrow.beans.ConnectionManager">
	<property name="driver" value="${driver}" />
	<property name="url" value="${url}" />
	<property name="userName" value="${user}" />
	<property name="password" value="${pwd}" />
</bean>

Technologies Used

Find the software/technologies used in this example.

  1. Eclipse Oxygen IDE
  2. JDK 8
  3. Spring 5

Properties File

In driver.properties file I have put the Oracle database credentials in form of key and value.

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
user=root
pwd=root

Spring Beans

Create a bean class that uses injected values from the properties file.

ConnectionManager.java
package org.websparrow.beans;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionManager {

	// Generates Setters
	private String driver, url, userName, password;

	// business logic that actually uses the inject values
	public void createConnection() {
		Connection conn = null;
		try {

			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName, password);

			if (conn != null) {
				System.out.println("Properties file data successfully injected to the bean and connection object is created.");
				System.out.println("Connection object is: " + conn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Spring Beans Configuration

The beans configuration file for loading the properties file into the context scope and inject it to the required bean or class.

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

	<!-- load properties file data into IOC context scope -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="driver.properties" />
	</bean>

	<!-- inject context scope data into required bean/class -->
	<bean id="connect" class="org.websparrow.beans.ConnectionManager">
		<property name="driver" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="userName" value="${user}" />
		<property name="password" value="${pwd}" />
	</bean>

</beans>

Run it

Load the configuration file and run it.

package org.websparrow.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.ConnectionManager;

public class Test {

	public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("spring.xml");
		ConnectionManager cm = (ConnectionManager) ap.getBean("connect");
		cm.createConnection();

	}
}
Output:

You will get the following result on your console log.

Properties file data successfully injected to the bean and connection object is created.
Connection object is: oracle.jdbc.driver.T4CConnection@4eb7f003

Share this article on: