Spring Dependency Injection With Set Collection Example

Previously we have let about Spring Dependency Injection With List Collection, There will not be much of difference between list and set,except we will be having Set in all the places of list. Lets see how to inject values to the Set Collection. We will be injecting String values to a Set and Objects to another Set using <set> tag in our configuration file.

 <property name="stringSet">
     <set>
         <value>Welcome</value>
         <value>To</value>
         <value>JavaInterivewPoint</value>
     </set>
 </property>            

When our client calls the Library class bean id “library“, the below happens which is not actually visible

Set<String> set = new HashSet<String>();
set.add("Welcome");
set.add("To");
set.add("JavaInterviewPoint");

lets see the complete example.

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 Book.java, Library.java and ClientLogic.java under  com.javainterviewpoint folder.
  4. Place our configuration file SpringConfig.xml in the src directory

Book.java

Book class will have all the book details such title, author, publications and its corresponding POJO’s. 

package com.javainterviewpoint;

public class Book 
{
    private String title;
    private String author;
    private String publications;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPublications() {
        return publications;
    }
    public void setPublications(String publications) {
        this.publications = publications;
    }
}

Library.java

Library class has two Sets, stringSet which holds the String values and bookSet which can hold Book type of objects and its corresponding getters and setters.

package com.javainterviewpoint;

import java.util.Set;

public class Library 
{
    private Set stringSet;
    private Set bookSet;
    
    public Set getStringSet() {
        return stringSet;
    }
    public void setStringSet(Set stringSet) {
        this.stringSet = stringSet;
    }
    public Set getBookSet() {
        return bookSet;
    }
    public void setBookSet(Set bookSet) {
        this.bookSet = bookSet;
    }
}

SpringConfig.xml
In our configuration file we have defined seperate id for each bean Library and Book classes. Using the <set> tag we have set values to the properties of the Library class

<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="library" class="com.javainterviewpoint.Library">
         <property name="bookSet">
             <set>
                <ref bean="book1"/>
                <ref bean="book2"/>
             </set>
         </property>
         <property name="stringSet">
             <set>
                 <value>Welcome</value>
                 <value>To</value>
                 <value>JavaInterivewPoint</value>
              </set>
         </property>
    </bean>
    <bean id="book1" class="com.javainterviewpoint.Book">
       <property name="title" value="Core Spring" />
       <property name="author" value="JavaInterviewPoint" />
       <property name="publications" value="JIP" />
    </bean>
    <bean id="book2" class="com.javainterviewpoint.Book">
       <property name="title" value="Spring MVC" />
       <property name="author" value="JavaInterviewPoint" />
       <property name="publications" value="JIP" />
     </bean>
</beans>

ClientLogic.java

package com.javainterviewpoint;

import java.util.Iterator;
import java.util.Set;

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 Library Instance
        Library library = (Library)bf.getBean("library");
        //Get bookSet
        Set bookSet = library.getBookSet();
        //Lets print the properties of the Book
        Iterator it = bookSet.iterator();
        int flag=1;
        while(it.hasNext())
        {
            Book book = (Book)it.next();
            System.out.println("**Book"+flag+" Properties**");
            System.out.println("Book Title        : "+book.getTitle());
            System.out.println("Book Author       : "+book.getAuthor());
            System.out.println("Book Publications : "+book.getPublications());
            flag++;
        }
        //Lets print the primitives
        Set stringSet = library.getStringSet();
        System.out.println("Primitives set to Set : "+stringSet);
    }
}
  • Resource class reads our Configuration File(SpringConfig.xml)
  • BeanFactory class read all the bean definition mentioned in the config file.
  • Get the Library Class instance by calling the getBean() method over the bean factory.
  • As we have already injected values to both of the Set of the Library class through our Config file. We will call the corresponding Sets getter to get the values associated with it.

Output

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

**Book1 Properties**
Book Title        : Core Spring
Book Author       : JavaInterviewPoint
Book Publications : JIP
**Book2 Properties**
Book Title        : Spring MVC
Book Author       : JavaInterviewPoint
Book Publications : JIP
Primitives set to Set : [Welcome, To, JavaInterivewPoint]

Leave a Reply

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