Spring AOP的AspectJ注解

在本篇文章中,我将讲解如何将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!");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AngeliaZheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值