6

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.

Fiftoine
  • 271
  • 5
  • 17

2 Answers2

10

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')]
    }
}
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

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)

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43