Avatar of gudii9
gudii9
Flag for United States of America asked on

spring alias

hi,

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" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/portlet/context/WEB-INF/contextInclude.xml">
]>

<beans>
<bean id="triangle" class="org.gp.gpr.Trianglebj">
<property name="pointA" ref="zeroPoint"/>
<property name="pointB">

<bean class="org.gp.gpr.Point">
<property name="x" value="20"/>
<property name="y" value="0"/>
</bean>
</property>


<property name="pointC">
<bean id="point3" class="org.gp.gpr.Point">
<property name="x" value="-200"/>
<property name="y" value="0"/>
</bean>
</property>
</bean>

<bean id="zeroPoint" class="org.gp.gpr.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>





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



clinet java looks as below

package org.gp.gpr;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

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");
            trianglebj.draw();
      }

}
















trinagle obj looks as below

package org.gp.gpr;

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


      //      System.out.println(getType()+"triangle drawed"+getHeight());
            System.out.println("PointA=("+getPointA().getX()+","+getPointA().getY()+")");
            System.out.println("PointB=("+getPointB().getX()+","+getPointB().getY()+")");
      System.out.println("PointC=("+getPointC().getX()+","+getPointC().getY()+")");
      }

      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

package org.gp.gpr;

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
Java EEJavaJSP

Avatar of undefined
Last Comment
girionis

8/22/2022 - Mon
SOLUTION
Am P

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
mccarl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes