1

A spring bean can be injected in a Grails application using resources.groovy. However, I would like to inject a map of beans. Key would be String, Value would an actual bean. Idea is I am trying to do a strategy style pattern where there would be a look up to a map and a corresponding bean service invoked?

Is that possible - thanks.

More Than Five
  • 9,959
  • 21
  • 77
  • 127
  • 1
    What have you tried? Have you tried `def mymap = [:]` then adding to it `mymap['key'] = ref('someBeanA')` then setting the property on your service in `Resources.groovy`? What didn't work about that? (Hint: it should work). – Joshua Moore Jul 08 '15 at 12:46
  • where those beans should be defined in the 1st place? some code would definitely help – injecteer Jul 08 '15 at 16:13

1 Answers1

1

I had similar issue, but I wanted to inject a list of beans. This example works:

bean1(Bean1) {}

bean2(Bean2) {}

beansHolder (BeansHolder ) {
    beans = [bean1, bean2]
}

I think you can do the same for a map:

    beans = ['first': bean1, 'second': bean2]
Taras Kohut
  • 2,505
  • 3
  • 18
  • 42