Dónal
Dónal

Reputation: 187499

get reference to spring bean from class in src/groovy

I'm writing a Grails plugin that defines a Spring bean in the plugin descriptor

def doWithSpring = {
    myBean(MyBean)
}

I need to get a reference to this bean from another class in the plugin.

class Something {

  def doIt() {
    // I need to get a reference to myBean here. Is this the best way?
    MyBean myBean = ApplicationHolder.application.mainContext.getBean('myBean')
  }
}

Something is a class defined in src/groovy within the same plugin as the bean, but Something is not itself a spring bean. In Grails 1.3.7 is there a better way of achieving this than that shown above? I'm looking for a better way because I know the *Holder classes are deprecated in Grails 2.0

Upvotes: 2

Views: 3280

Answers (1)

user800014
user800014

Reputation:

In Grails 2.x exists the Holders utility class to get the grailsApplication and the applicationContext.

In 1.3.7 I think the option is create your own holder, as described here.

Upvotes: 1

Related Questions