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">
   <props>
     <prop key="admin">[email protected]</prop>
     <prop key="manager">[email protected]</prop>
   </props>
 </property>

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.

    commons-logging-1.1.1.jar
    spring-beans-3.2.9.RELEASE.jar
    spring-core-3.2.9.RELEASE.jar
    spring-context-3.2.9.RELEASE.jar
    spring-expression-3.2.9.RELEASE.jar

  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.java

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;
    }
}

SpringConfig.xml
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"
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-3.0.xsd">
 
 <bean id="security" class="com.javainterviewpoint.Security">
     <property name="props">
         <props>
             <prop key="admin">[email protected]</prop>
             <prop key="manager">[email protected]</prop>
         </props>
     </property>
  </bean>
</beans>

ClientLogic.java

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**");
        while(enumeration.hasMoreElements())
        {
            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.

Output

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 *