Spring Properties Dependency Injection Example


The previous Spring tutorial explained about default and required type collection dependency injection. In this tutorial, we are going to explain how to inject Properties dependency using XML file. To inject the Property type dependency, we can use the <props/> tag and its child tag <prop/> to pass the values in form of key and value.

You can inject the Properties value in two ways.

  1. Properties injection through the configuration file.
  2. Properties injection through the properties file.

Properties injection through a simple configuration file, DTD or XSD based configuration works fine.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <!--mapping of class-->
</beans>

But Properties injection through properties file, DTD will not support, you must need to use XSD schema and add the util namespace.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!--mapping of class-->
</beans>

1- Properties injection through the configuration file

Properties dependency injection can be achieved by using <props/> and <prop/> tag. It internally used the Map interface.

1.1 Spring Beans

Create a bean class that have Properties type declaration and generate its setter method. Finally, create a business logic method that actually uses the injection properties values.

PropDi.java
package org.websparrow.beans;

import java.util.Properties;
import java.util.Set;

public class PropDi {

	private Properties driver;

	public void setDriver(Properties driver) {
		this.driver = driver;
	}

	// business logic
	public void display() {
		System.out.println("MySQL driver details..........\n");

		Set<Object> keys = driver.keySet();

		for (Object key : keys) {
			System.out.println(key + "=" + driver.getProperty(key.toString()));
		}
	}
}

1.2 Spring Beans Configuration

Create an XML file and configure the bean class. Use the <property/> tag for setter-based DI and its nested child tag <props/> and <prop/> to inject the Properties values.

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
 "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="prop" class="org.websparrow.beans.PropDi">
		<property name="driver">
			<props>
				<prop key="class_path">com.mysql.jdbc.Driver</prop>
				<prop key="url">jdbc:mysql://127.0.0.1:3306/database</prop>
				<prop key="user_name">system</prop>
				<prop key="password">manager</prop>
			</props>
		</property>
	</bean>
</beans>

1.3 Run it

Load the configuration file and run it.

Client1.java
package org.websparrow.test;

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

public class Client1 {

	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		PropDi pro = (PropDi) context.getBean("prop");
		pro.display();
	}
}

1.4 Output

You will the following injected values on your console log.

MySQL driver details..........

class_path=com.mysql.jdbc.Driver
password=manager
url=jdbc:mysql://127.0.0.1:3306/database
user_name=system

2- Properties injection through the properties file

Dependency injection through a properties file can only achieve by using XSD schema based configuration and must need to declare util namespace. After that, you can pass the exact location of the properties file.

In my case, I have placed my properties file in the classpath.

<util:properties location="classpath:database.properties"></util:properties>

If your properties file is inside any package, pass the full qualified class path location.

<util:properties location="classpath:org/websparrow/resources/database.properties"></util:properties>

2.1 Spring Beans

The bean class is exactly the same as above bean class.

PropFileDi.java
package org.websparrow.beans;

import java.util.Properties;
import java.util.Set;

public class PropFileDi {

	private Properties driver;

	public void setDriver(Properties driver) {
		this.driver = driver;
	}

	// business logic
	public void display() {
		System.out.println("Oracle driver details..........\n");

		Set<Object> keys = driver.keySet();

		for (Object key : keys) {
			System.out.println(key + "=" + driver.getProperty(key.toString()));
		}
	}
}

2.2 Property File

Here are the properties file details.

class_path=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
user_name=tiger
password=scott

2.3 Spring Beans Configuration

Configure the bean’s class and pass the exaction location of the properties file.

file-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" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<bean id="propfile" class="org.websparrow.beans.PropFileDi">
		<property name="driver">
			<util:properties location="classpath:database.properties"></util:properties>
		</property>
	</bean>
</beans>

2.4 Run it

Load the confirmation file and run it. It will inject the values from the properties file.

Client2.java
package org.websparrow.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.PropDi;
import org.websparrow.beans.PropFileDi;

public class Client2 {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("file-spring.xml");

		PropFileDi profile = (PropFileDi) context.getBean("propfile");

		profile.display();
	}
}

2.5 Output

You will get the following output on your console log loaded from the properties file.

Oracle driver details..........

class_path=oracle.jdbc.driver.OracleDriver
password=scott
url=jdbc:oracle:thin:@localhost:1521:xe
user_name=tiger

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.