class GenericService implements GroovyInterceptable {Now invokeMethod will be called when executing any function defined in GenericService class. Also, if any class extends GenericService the AOP functionality will be extended to all methods defined in it. For example:
boolean transactional = true
//Logging aspect
def invokeMethod(String name, Object args){
log.info("Starting $metaClass.theClass $name $args")
def metaClass = InvokerHelper.getMetaClass(this)
def result = metaClass.invokeMethod(this, name, args)
log.info("Ending $metaClass.theClass $name")
return result
}
}
class CarService extends GenericService {Notice that CarService extends GenericService. Now all methods in CarService have the logging aspect applied to it.
boolean transactional = true
def getCar(String make, String model) {
if (make && !model) {
return Car.findByMake(make)
} else if (!make && model) {
return Car.findByModel(model)
} else {
return Car.findByMakeAndModel(make, model);
}
}
}
Also, if A.foo() calls B.bar() and both A and B extend GenericService then the invoke method is executed for both A.foo and B.bar.
For example:
class AnotherService extends GenericService {
boolean transactional = true
def carService
def demo() {
carService.getCar("Toyota", "Camry")
}
}
Below is the output:
INFO [main]: ---------Test: testAnotherService start.---------The example code is available in GitHub: http://github.com/RaviH/GroovyAOPDemoInGrails
INFO [main]: Starting class com.mayabansi.demo.AnotherService demo []
INFO [main]: Starting class com.mayabansi.demo.CarService getCar [Toyota, Camry]
INFO [main]: Ending class com.mayabansi.demo.CarService getCar
INFO [main]: Ending class com.mayabansi.demo.AnotherService demo
INFO [main]: ---------Test: testAnotherService end.---------
Comments, feedback? Let me know...