troubleshooting Question

spring alias

Avatar of gudii9
gudii9Flag for United States of America asked on
JavaJSPJava EE
3 Comments3 Solutions2620 ViewsLast Modified:

I am running below example on spring alias

spring.xml looks as below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/portlet/context/WEB-INF/contextInclude.xml">

<bean id="triangle" class="">
<property name="pointA" ref="zeroPoint"/>
<property name="pointB">

<bean class="">
<property name="x" value="20"/>
<property name="y" value="0"/>

<property name="pointC">
<bean id="point3" class="">
<property name="x" value="-200"/>
<property name="y" value="0"/>

<bean id="zeroPoint" class="">
<property name="x" value="0"/>
<property name="y" value="0"/>

<alias name="triangle" alias="triangle-alias"/>

clinet java looks as below


import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;

public class DrawingAppObj {

       * @param args
      public static void main(String[] args) {
            //      Triangle triangle=new Triangle();
            //      triangle.draw();

//            BeanFactory factory=new XmlBeanFactory(new FileSystemResource("spring.xml"));
            ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
      //      Trianglebj trianglebj      =(Trianglebj) context.getBean("triangle");
            Trianglebj trianglebj      =(Trianglebj) context.getBean("triangle-alias");


trinagle obj looks as below


public class Trianglebj {
private Point pointA;
private Point pointB;
private Point pointC;
      public void draw(){

      //      System.out.println(getType()+"triangle drawed"+getHeight());

      public Point getPointA() {
            return pointA;

      public void setPointA(Point pointA) {
            this.pointA = pointA;

      public Point getPointB() {
            return pointB;

      public void setPointB(Point pointB) {
            this.pointB = pointB;

      public Point getPointC() {
            return pointC;

      public void setPointC(Point pointC) {
            this.pointC = pointC;


point class looks as below


public class Point {
private int x;
public int getX() {
      return x;
public void setX(int x) {
      this.x = x;
public int getY() {
      return y;
public void setY(int y) {
      this.y = y;
private int y;


My question is what is the purpose of the alias when we have bean id in spring bean definition. I see name also. what is the purposae of name. when we use id?name?alias name? which one is preferred to use

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
IT Business Systems Analyst / Software Developer
Join our community to see this answer!
Unlock 3 Answers and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros