Skip to content

Allow <idref/> to set property value to id of bean [SPR-2500] #7189

@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions