Spring AOP AspectJ annotations

In this article, I will explain how to integrate AspectJ annotations into the Spring AOP framework. In this Spring AOP+ AspectJ example, you will be freed from the tedious configuration of the previous article, making it easy to implement interception methods.

Common AspectJ annotation:

  • @Before – The method runs before execution.
  • @After – Run after the method returns the result.
  • @AfterReturning – Runs after the method returns a result, the result is returned in the interceptor.
  • @AfterThrowing - Run method after throwing an exception.
  • @Around – Run around the method, combining the three notifications above.

still uses the previous example, adding AspectJ jar dependencies on the previous project

<!-- AspectJ depends on jar -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>

Enable AspectJ

in the Spring configuration file, add "<aop:aspectj-autoproxy />"

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<aop:aspectj-autoproxy />
        ...
	
</beans>

AspectJ @Before

In the following example, the before() method will be in the ArticleService interface's queryArticleById( The execution of the method is performed before execution. AspectJ's "cut point" is used to declare which method will be intercepted. The

@Aspect
public class AspectJDemo {
    
    @Before("execution(* com.angelia.spring.service.ArticleServiceImpl.queryArticleById(..))")
    public void before(JoinPoint point) {
        System.out.println(point.getSignature().getName());
        System.out.println("before is running!");
    }

}

AspectJ @After

after() method will be executed after the execution of the queryArticleById() method of the ArticleService interface. The

@After("execution(* com.angelia.spring.service.ArticleServiceImpl.queryArticleById(..))")
public void after(JoinPoint point) {
	System.out.println(point.getSignature().getName());
	System.out.println("after is running!");
}

AspectJ @AfterReturning

afterReturning() method will be executed after the queryArticleById() method of the ArticleService interface is executed. In addition, you can also intercept the returned value using the "returning" attribute. To intercept the returned value, the same method argument (result) must be used for the value of the "returning" attribute (result).

@AfterReturning(
		pointcut = "execution(* com.angelia.spring.service.ArticleServiceImpl.queryArticleById(..))",
		returning= "result")
public void afterReturning(JoinPoint point, Object result) {
	System.out.println("afterReturning() is running!");
	System.out.println(point.getSignature().getName());
	System.out.println("Method returned value is : " + result);
}

AspectJ @AfterThrowing

If the queryArticleById() method of the ArticleService interface throws an exception, the afterThrowing() method will be executed.

@AfterThrowing(
		pointcut = "execution(* com.angelia.spring.service.ArticleServiceImpl.queryArticleById(..))",
		throwing= "error")
public void afterThrowing(JoinPoint point, Throwable error) {
	System.out.println("logAfterThrowing() is running!");
	System.out.println(point.getSignature().getName());
	System.out.println("Exception : " + error);
}

AspectJ @Around

In the following example, the around() method will be executed when the queryArticleById() method of the ArticleService interface is executed. You must define "point.proceed();" to control when the interceptor returns control to the original queryArticleById() method.

@Around("execution(* com.angelia.spring.service.ArticleServiceImpl.queryArticleById(..))")
Public void around(ProceedingJoinPoint point) throws Throwable {

System.out.println("around() is running!");
System.out.println(point.getSignature().getName());

System.out.println("Around before is running!");
Point.proceed(); //Continue to execute the original method
System.out.println("Around after is running!");
}