-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
George Licquorice opened SPR-2500 and commented
It is often useful to set a property of a bean to the ID of the bean. For example, the bean might have an ID property and when it is used with Spring, it would make sense for this to be set to the bean ID.
This can currently be done by using <idref> in the following way;
<bean id="myclass" class="org.myorg.MyClass" abstract="true">
... (set common properties)
</bean>
<bean id="myclass1" parent="myclass"/>
<property name="id"><idref local="myclass1"/></property>
...
</bean>
<bean id="myclass2" parent="myclass"/>
<property name="id"><idref local="myclass2"/></property>
...
</bean>
This is quite ugly however since each bean definition requires the id to be explicitly set.
I could make my bean class implement BeanNameAware and set the id based on this. While this avoids having to set the property in the config, it ties my bean class to Spring.
An alternative option would be to make <idref/> work without any attributes. In this situation it would refer to the id of the current bean. If this bean is the parent of another bean, it could resolve to the id of whatever the outermost bean is.
The definitions above would become something like this;
<bean id="myclass" class="org.myorg.MyClass" abstract="true">
<property name="id"><idref/></property>
...
</bean>
<bean id="myclass1" parent="myclass"/>
...
</bean>
<bean id="myclass2" parent="myclass"/>
...
</bean>
This is much cleaner and the bean class doesn't need any knowledge of Spring.
Affects: 2.0 RC2
Issue Links:
- Evaluate 'idref' element and 'local' attribute in Spring 3.1 [SPR-8179] #12830 Evaluate 'idref' element and 'local' attribute in Spring 3.1