First of all, you want to define your strategy in your grails-app/conf/spring/resources.groovy :
beans = { myStrat(com.yourcompany.StrategyImpl) { someProperty = someValue } }
Then you simply def property with the same name in your service:
class SomeGrailsService { def myStrat def someMethod() { return myStrat.doSomething() } }
In any Grails artifact (such as services and domain classes), Grails will automatically set the myStrat property myStrat correct value. But do not forget that in unit test you will have to specify this value manually, since automatic posting is not performed in unit tests.
Outside of the Grails artifact, you can use something like:
def myStrat = ApplicationHolder.application.mainContext.myStrat
In Grails 2.0, Graeme et al. Do not recommend using the *Holder classes (e.g. ApplicationHolder and ConfigurationHolder ), so I'm not quite sure what the Grails 2.0 approach will be ...
Adam c
source share