April 10, 2020
Estimated Post Reading Time ~

Integrating Spring AOP with AEM

Spring AOP
Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects. Some of the cases where we can use it are to control some of the common things like logging, adding some extra features 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 the various configurations we need to make.
Configuration at pom level
In the plugin, section add the following lines of code

<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>

Independency section add
<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>

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

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()+"()");
}
}

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

Source: https://www.tothenew.com/blog/integrating-spring-aop-with-aem/


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.