Spring Dependency Injection With Properties Example

We have already seen about how to inject dependency to a List, Set and Map Collection, now we will look on injection to a java util Property. Like Map we have key and values associated to the Property as well. We will be using <props> tag in our configuration file to inject values.

 <property name="props">
     <prop key="admin">[email protected]</prop>
     <prop key="manager">[email protected]</prop>

Here the we have configured 2 key,value pairs(admin and manager)
Folder Structure:

  1. Create a new Java Project  SpringCoreTutorial” and create a package for our src files com.javainterviewpoint
  2. Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.


  3. Create the Java classes Security.java and ClientLogic.java under  com.javainterviewpoint folder.
  4. Place our configuration file SpringConfig.xml in the src directory


Security class will have all Java util Properties props and its corresponding POJO’s. 

package com.javainterviewpoint;

import java.util.Properties;

public class Security 
    private Properties props;

    public Properties getProps() {
        return props;
    public void setProps(Properties props) {
        this.props = props;

In our configuration file we have put an entry for the Security class and inject the values to the props property using <props> tag.

 <beans xmlns="http://www.springframework.org/schema/beans"
 <bean id="security" class="com.javainterviewpoint.Security">
     <property name="props">
             <prop key="admin">[email protected]</prop>
             <prop key="manager">[email protected]</prop>


package com.javainterviewpoint;

import java.util.Enumeration;
import java.util.Properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientLogic 
    public static void main(String args[])
        //Read the configuration file
        Resource resource = new ClassPathResource("SpringConfig.xml");
        //Read all the bean definition
        BeanFactory bf = new XmlBeanFactory(resource);
        //Get the Security class Instance
        Security security = (Security)bf.getBean("security");
        //Get the Properties
        Properties props = security.getProps();
        //All the keys are obtained
        Enumeration enumeration = props.keys();
        //Lets print all the keys and values
        System.out.println("**Keys and Values of the Property set**");
            String key = String.valueOf(enumeration.nextElement());
            System.out.println("Key : "+key+" Value : "+props.getProperty(key));
  • Resource class reads our Configuration File(SpringConfig.xml)
  • BeanFactory class read all the bean definition mentioned in the config file.
  • Get the Security Class instance by calling the getBean() method over the bean factory.
  • As we have already injected props property through our Config file. We will call the corresponding getters to get the values associated with it.


On running the ClientLogic.java we will get the below output

**Keys and Values of the Property set**
Key : admin Value : [email protected]
Key : manager Value : [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *