Integrating Spring AOP with AEM
Spring AOP
Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects.Some of cases where we can use it are to control some of common things like logging , adding some extra feature without actually touching the core logic.
This blog post will explain how to enable aspects in our AEM projects using aspectj-maven-plugin.
Below are the steps for various configuration we need to make.
Configuration at pom level
In plugin section add following lines of code
[xml]
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
[/xml]
In dependency section add
[xml]
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.7</version>
<scope>compile</scope>
</dependency>
<!– Day CQ5 WCM Dependencies –>
<!– Apache Sling Dependencies –>
<!– Apache Jackrabbit/JCR Dependencies –>
</dependencies>
[/xml]
The Aspect class need to placed under folder src/main/aspect.
sample project structure
Below is the sample class which is used to log messages whenever the execution of method starts and end for all java classes methods that are inside the package com.ig
[java]
package com.ig.aspects;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class LoggerAspect {
@Pointcut(value = "execution(public * com.ig.core.*.*(..))")
public void anyMethod() {
}
@Before(value = "anyMethod()")
public void logEntry(JoinPoint joinPoint) {
Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());
logger.info("Method Started…."+joinPoint.getSignature().getName()+"()");
}
@After(value = "anyMethod()")
public void logExit(JoinPoint joinPoint) {
Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());
logger.info(" Method Exited…"+joinPoint.getSignature().getName()+"()");
}
}
[/java]
execution(public * com.ig.core.*.*(..)) : the execution of any method defined in the core package or a sub-package
@Before Before advice It will execute before the method execution
@After After returning advice tt will execute after the method is returned a result.
Refer Spring’s AOP documentation for Joint Point, Advice, Pointcuts, etc. and depending upon the use case in your application select the right one.
References:
http://docs.spring.io/spring/docs/2.5.5/reference/aop.html
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
I followed step by step at your recommendations but
after build and install bundle – logger not work.
Maybe we need to activate somehow this aspect?
I have followed the exact steps but LoggerAspect cxlass isn’t getting invoked ..
Can you guys please suggest me on this ?
I Have followed the same process but no luck.
LoggerAspect.java is not getting called in anycase.
could you guys please suggest me on this .
Hi Yagyesh,
Thanks for the Article,
but when i tried the same with my AEM project there were many errors while building it .
I was able to resolve them but it was throwing the error after adding the file LoggerAspect.java as “package org.aspectj.lang.annotation does not exist.”