Friday 17 March 2017

Spring: Inject bean using p-namespace

In my previous post, I explained how to inject properties to a bean using p-namespace. In this post, I am going to explain how to inject bean as property to other bean using p-namespace.

Exmaple
 <bean name="osho" class="com.sample.pojo.Author" p:firstName="Chandra Mohan"
  p:lastName="Jain" p:country="India" p:dateOfBirth="11 December 1931" />
  
 <bean name="book1" class="com.sample.pojo.Book" 
  p:title="Vedanta An Art of Dying"
  p:noOfPages = "164"
  p:price = "90"
  p:author-ref="osho" />

Notify above snippet property ‘author’ of the instance book1 is defined as p:author-ref="osho". In this case author is the property name, whereas the -ref part indicates that this is not a straight value but rather a reference to another bean.

Following is the complete working application.

Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

Book.java
package com.sample.pojo;

public class Book {
 private String title;
 private int noOfPages;
 private float price;
 private Author author;

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public int getNoOfPages() {
  return noOfPages;
 }

 public void setNoOfPages(int noOfPages) {
  this.noOfPages = noOfPages;
 }

 public float getPrice() {
  return price;
 }

 public void setPrice(float price) {
  this.price = price;
 }

 public Author getAuthor() {
  return author;
 }

 public void setAuthor(Author author) {
  this.author = author;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Book [title=").append(title).append(", noOfPages=").append(noOfPages).append(", price=")
    .append(price).append(", author=").append(author).append("]");
  return builder.toString();
 }

}

myConfiguration.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" xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean name="osho" class="com.sample.pojo.Author" p:firstName="Chandra Mohan"
  p:lastName="Jain" p:country="India" p:dateOfBirth="11 December 1931" />

 <bean name="book1" class="com.sample.pojo.Book" p:title="Vedanta An Art of Dying"
  p:noOfPages="164" p:price="90" p:author-ref="osho" />

</beans>

HelloWorld.java

package com.sample.test;

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

import com.sample.pojo.Book;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Book book = context.getBean("book1", Book.class);

  System.out.println(book);

  ((ClassPathXmlApplicationContext) context).close();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment