Integrating Spring AOP with Grails Application

27 / Aug / 2012 by Mohit Garg 1 comments

AOP means Aspect Oriented Programming

  1. Enables encapsulation of functionality that affects multiple classes in separate units.
  2. Complements Object Oriented Programming.
  3. 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

  1. Logging
  2. Transaction Management
  3. Profiling
  4. Security

What does Spring AOP contains:

  1. Join Point : A point in the execution of program.
  2. Advice : Code to be executed at a join point that has been selected by point cut.
  3. 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

  1. Firstly we have to add configuration of Spring AOP in resources.groovy
  2. Now write following line of codes in resources.groovy
beans = {
xmlns aop:"http://www.springframework.org/schema/aop"
aspectBean(com.test.aop.LoggerInterceptor)
aop.config("proxy-target-class":true) {
}

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.

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);
}
}

@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

Tag -

AOP Grails Spring
FOUND THIS USEFUL? SHARE IT

comments (1 “Integrating Spring AOP with Grails Application”)

  1. Joás Garcia

    Great post! I’ve created this repo to demonstrate the use of Spring AOP in a Grails application: github.com/joasgarcia/production-only-annotation

    Reply

Leave a comment -