But using your existing Java code in a Grails application is no problem. Grails is based on Spring and Groovy and thus there are some easy ways to embbed Java code to your app.
- You can create a normal Grails controller or service and import your Java classes the normal way. This works pretty easy, if you're developing your app mainly with Groovy.
- If your app is allready based on Spring and thus provides allready Spring services, you can embbed'em directly and reference them in your Grails controller or services.
To use it, you can put some Groovy code to the PROJECT_HOME/grails-app/conf/spring/resources.groovy file.
The following code shows you an example resources.groovy file:
import com.duckmaps.server.imagerendering.services.ImageServiceImplHere you're seeing, that a bean with name imageService and the class ImageServiceImpl is defined. By setting the property scope to session, Spring instantiates this class for each session. It is important to put the import statement to the top. If you forget it, your code won't work.
beans = {
imageService(ImageServiceImpl) {bean ->
bean.scope = "session"
}
}
Referencing this bean is the same as XML declared beans. Just put a
def imageServiceto your controller and Grails will inject it like a normal Grails controller.
No comments:
Post a Comment