Grails & Spring - in resources.groovy how to set up a list

The question is straightforward, how to create a bean list in .groovy resources?

Something like this does not work:

beans {
    listHolder(ListHolder){
        items = list(){
            item1(Item1),
            item2(Item2),
            ...
        }
    }
}

Thanks in advance for your help.

+5
source share
2 answers

If you need a list of links to other named beans, you can simply use the standard Groovy list notation, and everything will be resolved correctly:

beans {
    listHolder(ListHolder){
        items = [item1, item2]
    }
}

but this does not work when the "elements" must be anonymous inside the beans, which is equivalent to 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 will need to do something like

beans {
    'listHolder-item-1'(Item1)
    'listHolder-item-2'(Item2)

    listHolder(ListHolder){
        items = [ref('listHolder-item-1'), ref('listHolder-item-2')]
    }
}
+9
source

This is easy:

beans {
    item1(Item)
    item2(Item)
    listHolder(ListHolder) {
        items = [item1, item2]
    }
}

[ Spring Beans DSL] (http://grails.org/doc/latest/guide/spring.html#14.3 Runtime Spring Beans DSL)

+1

All Articles