Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

grails

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.

like image 661
Fiftoine Avatar asked Apr 05 '13 10:04

Fiftoine


2 Answers

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')]
    }
}
like image 191
Ian Roberts Avatar answered Nov 15 '22 09:11

Ian Roberts


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)

like image 40
Sergey Ponomarev Avatar answered Nov 15 '22 11:11

Sergey Ponomarev