Integrating Spring AOP with Grails Application
AOP means Aspect Oriented Programming
- Enables encapsulation of functionality that affects multiple classes in separate units.
- Complements Object Oriented Programming.
- Cross Cutting Concerns : Functionality whose implementation spans multiple modules.
Different From OOPS
Sometimes we want to implement common functionality through out some classes, through out some common method. We can achieve that functionality by implementing separate module without touching core logic.
Take the example of Banking Domain
In this case when person calls the withDraw() function, we want to log the detail of each step, that is performed in back end, i.e. what parameters were passed to each function, we want to log such details. Using OOPS concepts, we have to write logging code in each method, it’s very difficult to implement, but by using AOP we can implement this functionality as separate module without touching the core logic. We can implement security on method level by using some pattern as a Join Point with AOP.
Where can we use AOP
- Logging
- Transaction Management
- Profiling
- Security
What does Spring AOP contains:
- Join Point : A point in the execution of program.
- Advice : Code to be executed at a join point that has been selected by point cut.
- Point cut : An expression mapped to a join point.
Now Question comes in mind, How to integrate Spring AOP with Grails Application
It’s very easy to integrate Spring AOP with Grails Application
- Firstly we have to add configuration of Spring AOP in resources.groovy
- Now write following line of codes in resources.groovy
[groovy]
beans = {
xmlns aop:"http://www.springframework.org/schema/aop"
aspectBean(com.test.aop.LoggerInterceptor)
aop.config("proxy-target-class":true) {
}
[/groovy]
3. In this case we create the bean of AOP class by using aspectBean(com.test.aop.LoggerInterceptor).
4. aop.config(“proxy-target-class”:true) It’s enable AOP to create proxy of target class.
5. Create Groovy class that contains the implementation of AOP.
6. I will show the example of logging functionality with using AOP.
[groovy]
package com.test.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
import java.util.Arrays;
@Aspect
public class LoggerInterceptor {
private static Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);
@Before("within(com.service..*)")
public void logBefore(JoinPoint joinPoint){
String logMessage = String.format("Beginning of each method: %s.%s(%s)",
joinPoint.getTarget().getClass().getName(),
joinPoint.getSignature().getName(),
Arrays.toString(joinPoint.getArgs()));
logger.info(logMessage);
}
}
[/groovy]
@Aspect : Any bean with a class annotated as an aspect will be automatically detected by Spring.
@Before : It’s Before advice that executes before the matched method.
JoinPoint : Provides access to the current join point (target object, description of advised method,parameters types, parameters values, etc).
within(com.service..*) : This method will executed before calling any method in service class under package com.service..*
You can easily apply advice to any public method, any parameterized method to any Class.
To know more about AOP, you can take the reference of Spring AOP official site.
http://static.springsource.org/spring/docs/2.5.5/reference/aop.html
Thanks & Regards,
Mohit Garg
mohit@intelligrape.com
@gargmohit143
Great post! I’ve created this repo to demonstrate the use of Spring AOP in a Grails application: github.com/joasgarcia/production-only-annotation