views:

103

answers:

4

Hi, how can I call a method residing in an existing Java class from Grails app ? Is it necessary/recommended to wrap that up in a service?

+3  A: 

Hi,

is the class in a JAR file in your lib/ folder or in a .java file your src/ folder?

Either way, there's no need to wrap it in a service. You can construct instances of the class and call methods on it from anywhere.

If you need to wrap the method calls in a transaction then I'd put it in a service. Otherwise just call it directly. The only other reason to put it in a service is to use the scoping functionality (i.e. if you wanted a new instance of the class created for each request)

cheers

Lee

leebutts
+2  A: 

Put your source in src/java. Then in conf/spring/resources.groovy, you can do, for example:

// Place your Spring DSL code here
beans = {
    myJavaFunction(com.my.javafunction)

}

Then you can inject it into your controllers or services with:

def myJavaFunction
Brad Rhoads
Thanks for your reply. One more thing: I have an apache logger in this class and the compiler obviously doesn't find the logger jar. Do I just add the jar to the lib folder or get some special instance from the environment ?
xain
You might want to post that as separate question. I think we ran into that, but it wasn't worth spending time on at the moment. Grails has logging built in, so I guess there's some conflict.
Brad Rhoads
A: 

If you have them packed in a .jar file then just drop the jar file into your-project/lib

Luixv
A: 

there is no need to wrap as service. if you are using spring,just add the bean into resource.groovy

Ford Guo