Saturday, 2 September 2017

What is the use of Bean aliases and Bean name ?


Bean alias is another ID given to a bean and can be used as another bean ID for same object creation.
Aliases can be defined using 2 ways : Using alias tag and using bean name attribute

Example 1. Using alias tag
XML
<bean id="triangle" ..>
</bean>

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


USAGE
BeanFactory factory = new XmlBeanFactory(new   
FileSystemResource("spring.xml"));

Triangle t = (Triangle) factory.getBean("triangle-alias");
t.draw();


Example 2. Using bean name attribute
XML
<bean id="triangle" .. name="triangle-alias">
</bean>



No comments:

Post a Comment

Note: only a member of this blog may post a comment.