This application is the example of been collaboration that means one bean injecting the information of other beans
Book.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package s2; public class Book { private int bid; private String bname; private double price; public Book(){} public Book(int bid,String bname,double price) { this.bid=bid; this.bname=bname; this.price=price; } public String toString() { return bid+" "+bname+" "+price; } public void setBid(int n){ bid=n; } public int getBid(){ return bid; } public void setBname(String s){ bname=s; } public String getBname(){ return bname; } public void setPrice(double d){ price=d; } public double getPrice(){ return price; } } |
This is normal pojo class
BookInter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package s2; import java.util.*; public interface BookInter { public Book getBook(int id); public List getBooks(); } This is normal interface which contain only abstract methods 3 BookInterImpl.java package s2; import java.util.*; public class BookInterImpl implements BookInter { private List books=new ArrayList(); public BookInterImpl() { books.add(new Book(1001,"java",560)); books.add(new Book(1002,"oracle",450)); books.add(new Book(1003,"j2ee",700)); books.add(new Book(1004,"j2me",1560)); } public Book getBook(int id) { Iterator i1=books.iterator(); while(i1.hasNext()) { Book b=(Book)i1.next(); if(b.getBid() == id) return b; } return null; } public List getBooks() { return books; } } |
this is implementation details class file
CommandLineView.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package s2; import java.util.*; public class CommandLineView { BookInter binter; public void setBinter(BookInter b1) { binter=b1; } public void printBook(int id) { System.out.println("PARTICULAR BOOK INFORMATION :"+id); System.out.println(binter.getBook(id)); } public void printAllBooks() { System.out.println("ALL BOOKS INFORMATION.."); List books=binter.getBooks(); Iterator i1=books.iterator(); while(i1.hasNext()) { Book b=(Book)i1.next(); System.out.println(b); } } } |
this class displaying the out put.
book.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="b1" class="s2.BookInterImpl" > </bean> <bean id="c1" class="s2.CommandLineView" > <property name="binter"> <ref bean="b1" /> </property> </bean> </beans> This xml file is spring configuration file store all beans 5 ClientApp.java package s2; import org.springframework.core.io.*; import org.springframework.beans.factory.*; import org.springframework.beans.factory.xml.*; public class ClientApp { public static void main(String args[]) { Resource res=new ClassPathResource("s2/Book.xml"); BeanFactory factory=new XmlBeanFactory(res); CommandLineView clv=(CommandLineView)factory.getBean("c1"); clv.printBook(1002); clv.printAllBooks(); } } |
This is client application of all application
Output
1 2 3 4 5 6 7 | PARTICULAR BOOK INFORMATION :1002 1002 oracle 450.0 ALL BOOKS INFORMATION.. 1001 java 560.0 1002 oracle 450.0 1003 j2ee 700.0 1004 j2me 1560.0 |