Erik Sapir
Erik Sapir

Reputation: 24767

Injecting a Java class into Grails Groovy service

It is very easy to inject groovy services into other groovy services or controllers. However, i can't figure out how to inject a Java class into a groovy service.

Is it possible to perform such dependency injection?

Upvotes: 2

Views: 1230

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42264

Yes. All you have to do is to define your bean in resources.groovy like e.g.

beans = {
    //....
    myJavaComponent(com.example.YourJavaComponent) {
        //if it has any external dependencies you can define them as follow:
        externalDependency = ref('idOfTheBeanToInject')
}

Then all you have to do is just simply inject this bean into your Grails service by defining a property in class: def myJavaComponent, that's all.

See Using the Spring Bean DSL in the Grails docs

Upvotes: 5

Related Questions