How to give alias for a bean in Spring Configuration file?

In spring configuration file, we generally define bean declaration as follows,

<bean id="shape" class="com.saravanansivaji.spring.Shape">
<property name="shapes">
<list>
<ref bean="triangle"/>
<ref bean="circle"/>
</list>
</property>
</bean>

<bean id="triangle" class="com.saravanansivaji.spring.Triangle"/>
<bean id="circle" class="com.saravanansivaji.spring.Circle"/>

In the above bean declaration, bean ‘shape’ has a LIST which have two other beans as items such as ‘triangle’ and ‘circle’, using ref tag bean attribute.

Let we define an alias name for the bean ‘circle’

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

Re-writing the above configuration file is follows

<bean id="shape" class="com.saravanansivaji.spring.Shape">
<property name="shapes">
<list>
<ref bean="triangle"/>
<ref bean="circle-name"/>
</list>
</property>
</bean>

<bean id="triangle" class="com.saravanansivaji.spring.Triangle"/>
<bean id="circle" class="com.saravanansivaji.spring.Circle"/>

Did you notice, the list in the bean ‘shape’ refers the bean ‘circle’ with the alias-name, instead of id name.

Happy coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: