Wayne
Wayne

Reputation: 1145

Inject an AntBuilder using Spring?

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?

Upvotes: 0

Views: 161

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

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.

Upvotes: 0

Related Questions