We have learnt about Spring Setter Injection With Primitive data types in my previous post where we have set values to the primitives, Lets now see how to inject the objects through Spring setter injection.
Folder Structure:
- Create a new Java Project “SpringCoreTutorial” and create a package for our src files “com.javainterviewpoint“
- 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 - Create the Java classes Book.java, Library.java and ClientLogic.java under com.javainterviewpoint folder.
- 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. The getBookDetails() method will display the book information which is set.
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; } public void getBookDetails() { System.out.println("**Published Book Details**"); System.out.println("Book Title : "+title); System.out.println("Book Author : "+author); System.out.println("Book Publications : "+publications); } }
Library.java
Library class has the Book class instance as a property and its corresponding getters and setters. The book property will get its value through our configuration file.
package com.javainterviewpoint; public class Library { private Book book; public void setBook(Book book) { this.book = book; } public Book getBook() { return book; } }
SpringConfig.xml
In our configuration file we have defined seperate id for each bean Library and Book classes
<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="book" ref="book"></property> </bean> <bean id="book" class="com.javainterviewpoint.Book"> <property name="title" value="Spring Core"></property> <property name="author" value="JavaInterviewPoint"></property> <property name="publications" value="JIP"></property> </bean> </beans>
- We inject primitives to the Book class properties such as title, author, publications.
<bean id="book" class="com.javainterviewpoint.Book"> <property name="title" value="Spring Core"></property> <property name="author" value="JavaInterviewPoint"></property> <property name="publications" value="JIP"></property> </bean>
- For the library class we are referencing the Book class bean id to the property book.
<property name="book" ref="book"></property>
- The ref passed to the property book should be the bean id of the Book Class. In short
ref =<<bean id of Book class>>
ClientLogic.java
package com.javainterviewpoint; 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"); //Lets call the getBookDetails() method library.getBook().getBookDetails(); } }
- 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.
- The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
- As we have already injected Book object to the property book in Library class, we can call the getBookDetails() of the book class like “library.getBook().getBookDetails()” which displays the book details.
Output
Once we run our ClientLogic.java we will get the below output
**Published Book Details** Book Title : Spring Core Book Author : JavaInterviewPoint Book Publications : JIP
Leave a Reply