Monday, August 26, 2013

Inheriting Bean in Spring Example


Inheriting Bean in Spring Example

By using the parent attribute of bean, we can specify the inheritance relation between the beans.
Such case parent bean values will be inherited to the current bean.

Steps as follows:
      1. Create Address.java and Employee.java
      2. Create ApplicationContext.xml
      3. Create executable class called ExecuteEmployee.java

1. Create Address.java

package com;
import java.io.Serializable;

  public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    private String number;
    private String city;
    private String province;
    private String country;

    Address(String number, String city, String province, String country){
      super();
      this.number = number;
      this.city = city;
      this.province = province;
      this.country = country;
    }

    String displayAddressInfo(){
       return this.number + ", " + this.city + ", " + this.province + ", " + this.country;
    }
}

-------------------------------------------------

package com;
import java.io.Serializable;

public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;
    private String empID;
    private String empName;
    private Address empAddress;

    Employee(String empID, String empName){
       super();
       this.empID = empID;
       this.empName = empName;
    }

    Employee(String empID, String empName, Address empAddress){
       super();
       this.empID = empID;
       this.empName = empName;
       this.empAddress = empAddress;
    }

    void displayEmployee(){
       System.out.println("Employee ID : " + empID);
       System.out.println("Employee Name : " + empName);
       System.out.println("Address : " + empAddress.displayAddressInfo());
    }
}

2. 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="employee1Bean" class="com.Employee">
      <constructor-arg value="100100"></constructor-arg>
      <constructor-arg value="Sanjeeva Pathirana"></constructor-arg>
   </bean>

   <bean id="addressBean" class="com.Address">
      <constructor-arg value="126B, Pubudu Uyana"></constructor-arg>
      <constructor-arg value="Piliyandala"></constructor-arg>
      <constructor-arg value="Western Province"></constructor-arg>
      <constructor-arg value="Sri Lanka"></constructor-arg>
   </bean>

   <bean id="employee2Bean" class="com.Employee" parent="employee1Bean">
      <constructor-arg ref="addressBean"></constructor-arg>
   </bean>
</beans>

3. Create ExecuteEmployee.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 ExecuteEmployee {

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

   private void doExecute(){
     Resource resource = new    ClassPathResource("resource/ApplicationContext.xml");
     BeanFactory beanFactory = new XmlBeanFactory(resource);
     Employee employee = (Employee)beanFactory.getBean("employee2Bean");
     employee.displayEmployee();
   }
}


Out put as follows:

Employee ID : 100100
Employee Name : Sanjeeva Pathirana
Address : 126B, Pubudu Uyana, Piliyandala, Western Province, Sri Lanka

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