The question is straight forward, how to create a list of bean in resources.groovy?
Something like that doesn't work:
beans {
listHolder(ListHolder){
items = list(){
item1(Item1),
item2(Item2),
...
}
}
}
Thanks in advance for the help.
If you want a list of references to other named beans you can just use normal Groovy list notation and it will all be resolved properly:
beans {
listHolder(ListHolder){
items = [item1, item2]
}
}
but this doesn't work when the "items" need to be anonymous inner beans, the equivalent of the XML
<bean id="listHolder" class="com.example.ListHolder">
<property name="items">
<list>
<bean class="com.example.Item1" />
<bean class="com.example.Item2" />
</list>
</property>
</bean>
You'd have to do something like
beans {
'listHolder-item-1'(Item1)
'listHolder-item-2'(Item2)
listHolder(ListHolder){
items = [ref('listHolder-item-1'), ref('listHolder-item-2')]
}
}
It's easy:
beans {
item1(Item)
item2(Item)
listHolder(ListHolder) {
items = [item1, item2]
}
}
More details you can find in documentation of [Spring with the Beans DSL](http://grails.org/doc/latest/guide/spring.html#14.3 Runtime Spring with the Beans DSL)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With