tags:

views:

18

answers:

1

I have code scattered through a Service in a plugin, using AntBuilder like this:

def ant = new AntBuilder()

This results in code that is really hard to test I'd like to inject a prototype-scoped AntBuilder that I can replace with a mock. But, I'm pretty much a Spring newbie ... how would I do that?

I tried simply putting this in /grails-app/conf/spring/resources.groovy:

beans = {
 antBuilderBean(groovy.util.AntBuilder){b ->
        b.scope="prototype"
    }
}

and

class MyService {
  def antBuilderBean

but antBuilderBean is null. If I put the bean definition in the application's resources.groovy (rather than the plugin's resources.groovy), then the bean name shows up in the application context, but antBuilderBean still resoves to null in the service code.

Do I need some sort of a factory bean here? Or, do I have to explicitly build the bean from the Spring context or something?

A: 

This works for me in a new app just copy/pasting your code. Try running 'grails clean' - it often fixes issues with unexpected behavior like this.

Burt Beckwith
Thanks!I also found I could put the bean definition in the <my>GrailsPlugin.groovy doWithSpring() method.
Wayne
Sorry, didn't catch that this was in a plugin - doWithSpring() is the place to register beans in a plugin.
Burt Beckwith