Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use existing java class from grails

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?

like image 600
xain Avatar asked Dec 06 '22 03:12

xain


2 Answers

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
like image 129
Brad Rhoads Avatar answered Dec 07 '22 16:12

Brad Rhoads


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

like image 28
leebutts Avatar answered Dec 07 '22 15:12

leebutts