Spring Bean alias with Example

Spring bean has one or more identifiers. These identifiers must be unique within the spring container. A bean usually has only one identifier, but when sprig Bean requires more than one identifier the the extra identifier can be considered as aliases.
spring Bean alias means single spring bean can contain multiple names.
by followig way we can configure Bea alias.
<alias name = "xx" alias = "ysysy">
Tools Used In This Article
  • Spring 4.3.4
  • Maven 4.0.0
  • JDK 1.6
First create Maven Project from the Ecclipse and below is Project Directory Structure
Project Structure

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd
">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringaliasDemo</groupId>
<artifactId>SpringaliasDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
</project>
Laptop.java
package com.java2study;

import javax.annotation.Resource;


public class Laptop {
public String laptopName;
public String getLaptopName() {
return laptopName;
}
public void setLaptopName(String laptopName) {
this.laptopName = laptopName;
}

public void showName(){
System.out.println("The laptop Name is:"+laptopName);

}



}
spring-configuration.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-4.3.xsd"
>
<bean id="laptop" class="com.java2study.Laptop">

<property name="laptopName" value="AAAAA" />

</bean>
<alias name="laptop" alias="laptop_alias" />
<alias name="laptop" alias="laptop_alias1" />

</beans>
Test.java
package com.java2study;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class Test {
public static void main(String[] args) {
/*
* Resource resource = new
* ClassPathResource("spring-configuration.xml");
*/
Resource resource = new FileSystemResource(
"src/main/resources/spring-configuration.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Laptop laptop = (Laptop) beanFactory.getBean("laptop_alias");

laptop.showName();
System.out.println("--------------------------");

Laptop laptop1 = (Laptop) beanFactory.getBean("laptop_alias1");

laptop1.showName();

}

}
output
INFO: Loading XML bean definitions from file 
[/home/m/Spring4mvc/SpringaliasDemo/src/main/resources/spring-configuration.xml]
The laptop Name is:AAAAA
--------------------------
The laptop Name is:AAAAA

Download SourceCode


Spring Bean alias with Example source code