Showing posts with label 04. Constructor Injection with collections. Show all posts
Showing posts with label 04. Constructor Injection with collections. Show all posts

Monday, August 26, 2013

Constructor Injection with collections


Can inject collection values by constructor in Spring framework.
Can use with <constructor-arg>

1. list ( use list, set )


   <constructor-arg>
     <list>
       <value>94773020595</value>
       <value>94773020596</value>
       <value>94112613044</value>
     </list>
   </constructor-arg>

   <constructor-arg>
     <list>
        <ref bean="books1Bean"/>
        <ref bean="books2Bean"/>
        <ref bean="books3Bean"/>
        <ref bean="books4Bean"/>
     </list>
   </constructor-arg>

2. map

<constructor-arg>
   <map>
     <entry key="Sports Journal" value="TENs sport"> </entry>
     <entry key="Science" value="Gravity"></entry>
   </map>
</constructor-arg>

<constructor-arg>
   <map>
     <entry key-ref="newsPaper1Bean" value-ref="editor1Bean"></entry>
     <entry key-ref="newsPaper2Bean" value-ref="editor2Bean"></entry>
   </map>
</constructor-arg>

Find the example below.

Steps as follows:
      1. Create Books, Editor, NewsPaper, & Author java classes
      2. Create ApplicationContext emaple
      3. Create executable class called DisplayAuthorInformation.java
1. Create Books.java

package com;
import java.io.Serializable;

public class Books implements Serializable {

/**
*
*/
   private static final long serialVersionUID = 1L;
   private int bookRefId;
   private String bookCategory;
   private String bookName;
   private String publishedYear;
   
   Books(int refId, String category, String name, String year){
     super();
     this.bookRefId = refId;
     this.bookCategory = category;
     this.bookName = name;
     this.publishedYear = year;
   }

   public String displayBookDetails(){
     return "Reference ID : " + bookRefId + "\n\tCategory : " + bookCategory + "\n\tBook Name : " + bookName + "\n\tPublished Year : " + publishedYear;
   }

}

2. Create Editor.java

package com;

import java.io.Serializable;

public class Editor implements Serializable {

/**
*
*/
   private static final long serialVersionUID = 1L;
   private String editorRefId;
   private String editorName;
    
   public Editor(String editorRefId, String editorName) {
     super();
     this.editorRefId = editorRefId;
     this.editorName = editorName;
   }

   public String diplayEditor(){
     return "\n\tEditor Reference ID : " + editorRefId+ "\n\tEditor Name : " + editorName;
   }

}

3. Create NewsPaper.java

package com;

import java.io.Serializable;

public class NewsPaper implements Serializable {

/**
*
*/
   private static final long serialVersionUID = 1L;
   private String refID;
   private String paperName;
   private String day;

   public NewsPaper(String refID, String paperName, String day) {
    super();
    this.refID = refID;
    this.paperName = paperName;
    this.day = day;
   }

   public String diplayNewsPaper(){
     return "\tReference ID : " + refID + "\n\tPaper Name : " + paperName + "\n\tPublish Day : " + day;
   }

}

4. Create Author.java

package com;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Author implements Serializable {

/**
*
*/
   private static final long serialVersionUID = 1L;

   private String autherName;
   private List<String> contactInfo;
   private List<Books> bookList;
   private Map<String, String> journalsDetails;
   private Map<NewsPaper, Editor> papers;
  
   public Author(String aName, List<String> contactInfo, List<Books> bookList,
Map<String, String> journalsDetails, Map<NewsPaper, Editor> papers) {
     super();
     this.autherName = aName;
     this.contactInfo = contactInfo;
     this.bookList = bookList;
     this.journalsDetails = journalsDetails;
     this.papers = papers;
   }

   void displayAuthorDetails(){
     System.out.println("Author Name : " + autherName);

     System.out.println("\nContacts - ");    
     Iterator<String> itrContactList = contactInfo.iterator();
     while(itrContactList.hasNext()){
       System.out.println(" * " + itrContactList.next());
     }

     System.out.println("\nBooks - ");
     Iterator<Books> itrBookList = bookList.iterator();
     while(itrBookList.hasNext()){
       System.out.println("\t" + itrBookList.next().displayBookDetails() + "\n");
     }

     System.out.println("Journals - ");
     Set<Entry<String, String>> set = journalsDetails.entrySet();
     Iterator<Entry<String, String>> itrJournals = set.iterator();
     while(itrJournals.hasNext()){
       Entry<String, String> entry = itrJournals.next();
       System.out.println(" * " + entry.getKey() + " - " + entry.getValue());
     }

     System.out.println("\nPapers - ");
     Set<Entry<NewsPaper, Editor>> setPaper = papers.entrySet();
     Iterator<Entry<NewsPaper, Editor>> itrPaper = setPaper.iterator();
     while(itrPaper.hasNext()){
        Entry<NewsPaper, Editor> entry = itrPaper.next();
        NewsPaper newsPaper = entry.getKey();
        Editor editor = entry.getValue();
        System.out.println("\n* " + newsPaper.diplayNewsPaper() + "\n---\n " +    editor.diplayEditor());
     }
   }
}

 
5. Create ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="books1Bean" class="com.Books">
  <constructor-arg type="int" value="100103"></constructor-arg>
  <constructor-arg value="Programming"></constructor-arg>
  <constructor-arg value="Spring for every one"></constructor-arg>
  <constructor-arg value="2010"></constructor-arg>
</bean>

<bean id="books2Bean" class="com.Books">
  <constructor-arg type="int" value="100102"></constructor-arg>
  <constructor-arg value="Programming"></constructor-arg>
  <constructor-arg value="Struts 2"></constructor-arg>
  <constructor-arg value="2008"></constructor-arg>
</bean>

<bean id="books3Bean" class="com.Books">
  <constructor-arg type="int" value="100101"></constructor-arg>
  <constructor-arg value="Programming"></constructor-arg>
  <constructor-arg value="Hibernate"></constructor-arg>
  <constructor-arg value="2008"></constructor-arg>
</bean>
 
<bean id="books4Bean" class="com.Books">
  <constructor-arg type="int" value="100100"> </constructor-arg>
  <constructor-arg value="Network"></constructor-arg>
  <constructor-arg value="TCP/IP"></constructor-arg>
  <constructor-arg value="2009"></constructor-arg>
</bean>
 
<bean id="newsPaper1Bean" class="com.NewsPaper">
  <constructor-arg value="NP001"></constructor-arg>
  <constructor-arg value="Sunday Observer"></constructor-arg>
  <constructor-arg value="Sunday"></constructor-arg>
</bean>

<bean id="newsPaper2Bean" class="com.NewsPaper">
  <constructor-arg value="NP002"></constructor-arg>
  <constructor-arg value="Times"></constructor-arg>
  <constructor-arg value="Monday"></constructor-arg>
</bean>

<bean id="editor1Bean" class="com.Editor">
  <constructor-arg value="ED0001"></constructor-arg>
  <constructor-arg value="Sandamali Silva"></constructor-arg>
</bean>

<bean id="editor2Bean" class="com.Editor">
  <constructor-arg value="ED0002"></constructor-arg>
  <constructor-arg value="Thinuli Pathirana"></constructor-arg>
</bean>
 
<bean id="authorBean" class="com.Author">

  <constructor-arg value="Sanjeeva Pathirana"></constructor-arg>
  <constructor-arg>
    <list>
      <value>94773020595</value>
      <value>94773020596</value>
      <value>94112613044</value>
    </list>
  </constructor-arg>
  <constructor-arg>
    <list>
       <ref bean="books1Bean"/>
       <ref bean="books2Bean"/>
       <ref bean="books3Bean"/>
       <ref bean="books4Bean"/>
    </list>
  </constructor-arg>

  <constructor-arg>
     <map>
        <entry key="Sports Journal" value="TENs sport"></entry>
        <entry key="Science" value="Gravity"></entry>
     </map>
  </constructor-arg>
   
  <constructor-arg>
     <map>
        <entry key-ref="newsPaper1Bean" value-ref="editor1Bean"></entry>
        <entry key-ref="newsPaper2Bean" value-ref="editor2Bean"></entry>
     </map>
   </constructor-arg>

</bean>
</beans>


6. Create DisplayAuthorInformation.java


package com;
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 DisplayAuthorInformation {

/**
* @param args
*/
   public static void main(String[] args) {
     DisplayAuthorInformation authorInformation = new    DisplayAuthorInformation();
     authorInformation.doExecute();
   }

   private void doExecute(){
     Resource resource = new ClassPathResource("xml/ApplicationContext.xml");
     BeanFactory beanFactory = new XmlBeanFactory(resource);
     Author authorDetails = (Author)beanFactory.getBean("authorBean");
     authorDetails.displayAuthorDetails();
   }
}


Out put as follows:

Author Name : Sanjeeva Pathirana

Contacts -
* 94773020595
* 94773020596
* 94112613044

Books -
Reference ID : 100103
Category : Programming
Book Name : Spring for every one
Published Year : 2010

Reference ID : 100102
Category : Programming
Book Name : Struts 2
Published Year : 2008

Reference ID : 100101
Category : Programming
Book Name : Hibernate
Published Year : 2008

Reference ID : 100100
Category : Network
Book Name : TCP/IP
Published Year : 2009

Journals -
* Sports Journal - TENs sport
* Science - Gravity

Papers -

* Reference ID : NP001
Paper Name : Sunday Observer
Publish Day : Sunday
---
Editor Reference ID : ED0001
Editor Name : Sandamali Silva

* Reference ID : NP002
Paper Name : Times
Publish Day : Monday
---
Editor Reference ID : ED0002
Editor Name : Thinuli Pathirana