Rajinder Menu

Dependency Injection Into Collection and Array Type Properties


This tutorial shows how to inject values into collection type properties of Spring beans i.e properties of type Collection,List, Set etc.

This example is using following technologies:

1) Spring-framework-3.0.2.RELEASE
2) Eclipse Java EE IDE Helios Release (Eclipse version 3.6)
3) Java SE 6  

Jars Used:
 org.springframework.beans-3.0.2.RELEASE.jar
 org.springframework.core-3.0.2.RELEASE.jar
 org.springframework.context-3.0.2.RELEASE.jar
 org.springframework.asm-3.0.2.RELEASE.jar
 org.springframework.expression-3.0.2.RELEASE.jar
 commons-logging-1.1.1.jar


Step 1: Create a new Project in eclipse.


Create a new project in eclipse by going to File > New > Java Project and name it as SpringDICollections.

Project structure in eclipse is shown below:




There are four files used in this program:

1)      BlogPost.java
2)      Image.java
3)      MainApp.java
4)      ApplicationContext.java

Step2: Add the source code.

Add the following source code in eclipse.

Blogpost.java

package examples.spring.di;

import java.util.List;
import java.util.Set;

public class BlogPost {
   /* List Type properties */
   List<String> tagsAssignedAsList;
   List<Image> imagesInBlogAsList;

   /* Set type properties*/
   Set<String> tagsAssignedAsSet;
   Set<Image> imagesInBlogAsSet;
  
   /* Array*/
   String[] authors;
  
   
    public void setTagsAssignedAsList(List<String> tagsAssignedAsList){
        this.tagsAssignedAsList=tagsAssignedAsList;
    }
   
    public List<String> getTagsAssignedAsList(){
        return (List<String>)tagsAssignedAsList;
    }
   
    public void setImagesInBlogAsList(List<Image> imagesInBlogAsList){
        this.imagesInBlogAsList=imagesInBlogAsList;
    }
   
    public List<Image> getImagesInBlogAsList(){
        return (List<Image>)imagesInBlogAsList;
    }
   
    public void setTagsAssignedAsSet(Set<String> tagsAssignedAsSet){
        this.tagsAssignedAsSet=tagsAssignedAsSet;
    }
   
    public Set<String> getTagsAssignedAsSet(){
        return (Set<String>)tagsAssignedAsSet;
       
    }
   
    public void setImagesInBlogAsSet(Set<Image> imagesInBlogAsSet){
        this.imagesInBlogAsSet=imagesInBlogAsSet;
    }
   
    public Set<Image> getImagesInBlogAsSet(){
        return (Set<Image>)imagesInBlogAsList;
    }

    public void setAuthors(String[] authors){
        this.authors=authors;
    }
   
    public String[] getAuthors(){
        return authors;
    }
   
    public void showBlogTags(){
        System.out.println("Tags Assigned To Blog As List:: ");   
        for(String tag:tagsAssignedAsList){
            System.out.println(tag);           
        }
   
        System.out.println("**************");
       
        System.out.println("Tags Assigned To Blog As Set:: ");   
        for(String tag:tagsAssignedAsSet){
        System.out.println(tag);           
        }
    }
   
    public void showBlogImages(){
        System.out.println("**************");
        System.out.println("Images Used In Blog As List:: ");   
        for(Image img:imagesInBlogAsList){
            System.out.println("Image ID:"+img.getImageId());           
        }
   
        System.out.println("**************");
       
        System.out.println("Images Used In Blog As Set:: ");   
        for(Image img:imagesInBlogAsSet){
            System.out.println("Image ID:"+img.getImageId());           
         }   
        }
    public void showBlogAuthors(){
        System.out.println("**************");
       
        System.out.println("Authors of Blog:: ");
       
        for(String author:authors){
            System.out.println(author);           
         }
    }
}


Image.java
package examples.spring.di;

public class Image {
    String imageId;
    String imageFileName;
  
    public void setImageId(String imageId){
        this.imageId=imageId;
    }
  
    public String getImageId()    {
        return imageId;
    }
  
    public void setImageFileName(String imageFileName){
        this.imageFileName=imageFileName;
    }
  
    public String getImageFileName()    {
        return imageFileName;
    }
  
}


MainApp.java
package examples.spring.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {    public static void main(String args[]){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
        BlogPost myBlog=(BlogPost)ctx.getBean("myBlogPost");
        myBlog.showBlogTags();
        myBlog.showBlogImages();
        myBlog.showBlogAuthors();
        }
}


ApplicationContext.java
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="image1" class="examples.spring.di.Image">
   <property name="imageId" value="image1"/>
   <property name="imageFileName" value="ImageFile1"/>
</bean>

<bean id="image2" class="examples.spring.di.Image">
   <property name="imageId" value="image2"/>
  <property name="imageFileName" value="ImageFile2"/>
</bean>

<bean id="image3" class="examples.spring.di.Image">
  <property name="imageId" value="image3"/>
  <property name="imageFileName" value="ImageFile3"/>
</bean>

<bean id="image4" class="examples.spring.di.Image">
  <property name="imageId" value="image4"/>
  <property name="imageFileName" value="ImageFile4"/>
</bean>


<bean id="myBlogPost" class="examples.spring.di.BlogPost">

    <!-- Injecting 'primitive values'  in List type property-->
    <property name="tagsAssignedAsList">
     <list>
         <value>Java</value>
         <value>Java</value> <!-- Duplicates will be accepted in List-->
         <value>Spring</value>
         <value>Spring-core</value>
         <value>Spring-examples</value>
     </list>
    </property>
  
    <!-- Injecting 'references to other beans' as dependencies-->
    <property name="imagesInBlogAsList">
     <list>
         <ref bean="image1"/>
         <ref bean="image2"/>
         <ref bean="image3"/>
         <ref bean="image4"/>
         <ref bean="image4"/> <!-- Duplicates will be accepted in List-->
     </list>
    </property>



    <!-- Injecting 'primitive values'  in Set type property-->
    <property name="tagsAssignedAsSet">
     <set>
         <value>Java</value>
         <value>Java</value> <!-- Duplicates will not get accepted in Set-->
         <value>Spring</value>
         <value>Spring-core</value>
         <value>Spring-examples</value>
     </set>
    </property>
  
    <!-- Injecting 'references to other beans' as dependencies-->
    <property name="imagesInBlogAsSet">
     <set>
         <ref bean="image1"/>
         <ref bean="image2"/>
         <ref bean="image3"/>
         <ref bean="image4"/>
         <ref bean="image4"/>  <!-- Duplicates will not get accepted in Set-->
     </set>
    </property>
  
    <!-- Injecting values into an 'array' -->
    <property name="authors">
     <list>
         <value>Mr. X</value>
         <value>Mr. Y</value>
     </list>
    </property>

<!-- <set> Can also be used for 'authors' instead of <list> AS:

    <property name="authors">
     <set>
         <value>Mr. X</value>
         <value>Mr. Y</value>
     </set>
    </property>
  
    In case of <set>, duplicates in array 'authors' will not get accepted.
 -->
  
</bean>

</beans>


Step3: Add jars

Add the above mentioned jars in your project by right clicking on your project SpringDICollections,  go to Build Path > Configure Build Path > Add External JARs....

You can download commons-logging-1.1.1.jar from here. Spring related jars can be found in dist folder of your downloaded Spring 3.0.2 distribution folder.

Step 4: Running the program.

Right Click on the project SpringDICollections and go to Run As > Java Application.

You will see the following output  in the Console Pane.

Output:

Tags Assigned To Blog As List::
Java
Java       [Note the duplicates are allowed!]
Spring
Spring-core
Spring-examples
**************
Tags Assigned To Blog As Set::
Java    [Note the duplicates are NOT allowed!]
Spring
Spring-core
Spring-examples
**************
Images Used In Blog As List::
Image ID:image1
Image ID:image2
Image ID:image3
Image ID:image4
Image ID:image4   [Note the duplicates are allowed!]
**************
Images Used In Blog As Set::
Image ID:image1
Image ID:image2
Image ID:image3
Image ID:image4  [ [Note the duplicates are NOT allowed!]]
**************
Authors of Blog::
Mr. X
Mr. Y


Hope this would be helpful!



I would like to know your comments and if you liked the article then please share it on social networking buttons.


No comments:

Post a Comment