在本篇文章中,我将讲解如何将AspectJ注解集成到Spring AOP框架。在这个Spring AOP+ AspectJ 例子中,将把你从前一篇文章的繁琐配置中解脱出来,让你轻松实现拦截方法。
常见AspectJ的注解:
- @Before – 方法执行前运行。
- @After – 运行在方法返回结果后。
- @AfterReturning – 运行在方法返回一个结果后,在拦截器返回结果。
- @AfterThrowing – 运行方法在抛出异常后。
- @Around – 围绕方法执行运行,结合以上这三个通知。
依然沿用之前的例子,在之前项目基础上添加AspectJ的jar依赖
<!-- AspectJ依赖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>
启用AspectJ
在 Spring 配置文件,添加“<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
在下面例子中,before()方法将在 ArticleService 接口的 queryArticleById() 方法的执行之前被执行。AspectJ的“切入点”是用来声明哪种方法将被拦截。
@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()方法将在 ArticleService 接口的 queryArticleById() 方法的执行之后执行。
@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()方法将在 ArticleService 接口的 queryArticleById() 方法执行之后执行。此外,还可以截取返回的值使用“returning”属性。要截取返回的值,对“returning”属性(结果)的值必须用相同的方法参数(结果)。
@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
如果 ArticleService 接口的 queryArticleById() 方法抛出异常 afterThrowing() 方法将被执行。
@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
在下面例子中,around()方法将在 ArticleService 接口的 queryArticleById() 方法执行之时执行, 必须定义“point.proceed();” 控制何时拦截器返回控制到原来的queryArticleById()方法。
@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(); //继续执行原来的方法
System.out.println("Around after is running!");
}