Thursday, May 7, 2009

Spring Alias

Spring supports add alias to an existing bean.

<alias name="fromName" alias="toName"/>

Let’s say we have two beans, settingService and cachedSettingService. The only difference is cachedSettingService will cache the data. A lot of other beans has cachedSettingService injected.

<bean name=”bean1”>
<property name=”settingService” ref=”cachedSettingService” />
<bean name=”bean2”>
<property name=”settingService” ref=”cachedSettingService” />

Say we found cachedSettingService did not work correctly and want to using settingService to replace it temporarily. Definitely we don’t want to change bean1, bean2… because eventually we will still use the cachedSettingService when the problem is solved. Now Alias can help us.

<alias name=”settingService” alias=”cachedSettingService” />

This will inject the instance of settingService into bean1, bean2.

No comments:

Post a Comment