Wednesday 1 February 2017

Spring: Aliasing a bean

In my previous post, I explained how to access bean with multiple names. There is also another way, by using aliases, you can refer a bean using other name.

Syntax
<alias name="fromName" alias="toName"/>

Bean with name fromName can be referred by using the alias name toName.

Example
 <bean id="author1" name="Osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

 <alias name="Osho" alias="Rajneesh" />
 <alias name="Osho" alias="Chandra Mohan Jain" />

You can refer the bean named “Osho” with ‘Ranjneesh” (or) “Chandra Mohan Jain”.

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();
 }

}

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

 <bean id="author1" name="Osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

 <alias name="Osho" alias="Rajneesh" />
 <alias name="Osho" alias="Chandra Mohan Jain" />

</beans>


HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Author;

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

  Author author1 = context.getBean("Rajneesh", Author.class);
  Author author2 = context.getBean("Osho", Author.class);
  Author author3 = context.getBean("Chandra Mohan Jain", Author.class);

  System.out.println(author1);
  System.out.println(author2);
  System.out.println(author3);

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

Run HelloWorld.java, you can able to see following output.
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]



Previous                                                 Next                                                 Home

No comments:

Post a Comment