I'm using Spring to define stages in my application. It's configured that the necessary class (here called Configurator
) is injected with the stages.
Now I need the List of Stages in another class, named LoginBean
. The Configurator
doesn't offer access to his List of Stages.
I cannot change the class Configurator
.
My Idea:
Define a new bean called Stages and inject it to Configurator
and LoginBean
.
My problem with this idea is that I don't know how to transform this property:
<property ...>
<list>
<bean ... >...</bean>
<bean ... >...</bean>
<bean ... >...</bean>
</list>
</property>
into a bean.
Something like this does not work:
<bean id="stages" class="java.util.ArrayList">
Can anybody help me with this?
And in SomeClass:
I think you may be looking for
org.springframework.beans.factory.config.ListFactoryBean
.You declare a ListFactoryBean instance, providing the list to be instantiated as a property withe a
<list>
element as its value, and give the bean anid
attribute. Then, each time you use the declaredid
as aref
or similar in some other bean declaration, a new copy of the list is instantiated. You can also specify theList
class to be used.Import the spring util namespace. Then you can define a list bean as follows:
The value-type is the generics type to be used, and is optional. You can also specify the list implementation class using the attribute
list-class
.Here is one method:
Use the util namespace, you will be able to register the list as a bean in your application context. You can then reuse the list to inject it in other bean definitions.
As an addition to Jakub's answer, if you plan to use JavaConfig, you can also autowire that way: