Spring学习通过AspectJ注解方式实现AOP操作
作者:把苹果咬哭的测试笔记 发布时间:2023-09-22 22:09:44
Spring注解AspectJ操作AOP
一、被增强类
新建一个被增强的类 User,下面有个 add() 方法。
package com.pingguo.spring5.aopanno;
public class User {
public void add() {
System.out.println("add ... ...");
}
}
二、增强类
创建增强类,用于编写增强的逻辑。
package com.pingguo.spring5.aopanno;public class UserProxy { // 前置通知 public void before() { System.out.println("before ... ..."); }}package com.pingguo.spring5.aopanno;
public class UserProxy {
// 前置通知
public void before() {
System.out.println("before ... ...");
}
}
三、进行通知的配置
1. spring 配置文件中,开启扫描。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.pingguo.spring5.aopanno"></context:component-scan>
<!--开启 Aspect 生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
这里创建了 2 个名称空间:
xmlns:context:开启注解扫描用
xmlns:aop:开启生成代理对象
2. 使用注解创建 User 和 UserProxy 对象
// 被增强类
@Component
public class User {
public void add() {
System.out.println("add ... ...");
}
}
// 增强类
@Component
public class UserProxy {
// 前置通知
public void before() {
System.out.println("before ... ...");
}
}
使用 @Component 注解。
3. 在增强类上使用注解 @Aspect
// 增强类
@Component
@Aspect
public class UserProxy {
// 前置通知
public void before() {
System.out.println("before ... ...");
}
}
4. spring配置,开启生成代理对象
<!--开启 Aspect 生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
在配置文件中增加配置。
5. 配置不同类型的通知
在上一篇文章中提到了 5 种不同类型的通知,在这里使用不同的注解来配置不同的类型。
(1)@Before
表示作为前置通知。
// 增强类
@Component
@Aspect
public class UserProxy {
// 前置通知
@Before(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void before() {
System.out.println("before ... ...");
}
}
@Before 注解里的 value 值就是切入点表达式,表示要对哪个类里面的哪个方法进行增强。
新建一个测试类的方法运行一下:
public class TestAop {
@Test
public void testAopanno() {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
User user = context.getBean("user", User.class);
user.add();
}
}
运行结果:
before ... ...
add ... ...
Process finished with exit code 0
可以看出,先执行了前置增强 before() 方法,再执行了 add() 方法。
(2)@After
表示作为后置通知。而且不管有没有异常都会执行(文末示例)。
// 后置通知
@After(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void after() {
System.out.println("After ... ...");
}
运行结果:
add ... ...
After ... ...
Process finished with exit code 0
(3)@AfterReturning
另外,还有个注解 @AfterReturning,也是在被增强之后执行,不过可以拿到被增强方法的返回值。
修改被增强类的 add() 方法:
// 被增强类
@Component
public class User {
public String add() {
System.out.println("add ... ...");
return "add()方法返回值";
}
}
修改增强类:
@AfterReturning(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))", returning = "result")
public void afterReturning(String result) {
System.out.println("AfterReturning ... ..." + result);
}
这里 returning = "result",result 就是定义的获取到的变量,下面可以使用。
运行测试:
add ... ...
AfterReturning ... ...add()方法返回值
Process finished with exit code 0
(4)@Around
表示环绕通知。
// 环绕通知
@Around(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前 ... ...");
// 被增强的方法执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后 ... ...");
}
运行结果:
环绕之前 ... ...
add ... ...
环绕之后 ... ...
Process finished with exit code 0
(5)@AfterThrowing
表示环绕通知。
现在让 add() 方法抛异常:
// 被增强类
@Component
public class User {
public void add() {
int i = 1/0;
System.out.println("add ... ...");
}
}
使用 @AfterThrowing:
// 异常通知
@AfterThrowing(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void afterThrowing() {
System.out.println("AfterThrowing ... ...");
}
运行测试:
AfterThrowing ... ...
java.lang.ArithmeticException: / by zero
注意,在上面提到的 @After,不管有没有异常都会执行。
// 异常通知
@AfterThrowing(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void afterThrowing() {
System.out.println("AfterThrowing ... ...");
}
// 后置通知
@After(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void after() {
System.out.println("After ... ...");
}
运行测试:
After ... ...
AfterThrowing ... ...
java.lang.ArithmeticException: / by zero
四、抽取相同切入点
在上述的介绍中,发现每个通知里的切入点表达式都是一样的,那么可以进行抽取。
修改增强类,使用 @Pointcut :
// 增强类
@Component
@Aspect
public class UserProxy {
@Pointcut(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void pointDemo() {
}
// 前置通知
@Before(value = "pointDemo()")
public void before() {
System.out.println("before ... ...");
}
... ...
使用 @Pointcut 注解把表达式抽取出来到方法 pointDemo() 上,后续的通知里,value = "pointDemo()" 即可。
运行测试:
before ... ...
add ... ...
Process finished with exit code 0
如果需要改动表达式,只修改这一处就好。
五、多个增强类的优先级
如果有多个增强类对同一个方法进行增强,可以设置增强类的优先级。
给这 2 个增强类添加注解 @Order(1)、 @Order(2),注意,里面的数值越小,优先级越高。
新建的增强类 PersonProxy:
// 新建另一个增强类
@Component
@Aspect
@Order(1)
public class PersonProxy {
@Pointcut(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void pointDemo() {
}
// 前置通知
@Before(value = "pointDemo()")
public void before() {
System.out.println("PersonProxy 类的 before ... ...");
}
}
之前的 增强类:
// 增强类
@Component
@Aspect
@Order(2)
public class UserProxy {
@Pointcut(value = "execution(* com.pingguo.spring5.aopanno.User.add(..))")
public void pointDemo() {
}
// 前置通知
@Before(value = "pointDemo()")
public void before() {
System.out.println("before ... ...");
}
运行测试:
PersonProxy 类的 before ... ...
before ... ...
add ... ...
Process finished with exit code 0
Order(1) 的增强了 PersonProxy 下的通知先执行。
来源:https://blog.csdn.net/wessonlan/article/details/124812973


猜你喜欢
- Android版本更新实例详解1、导入xutils的jar包 2、在AndroidManifest.xml中添加权限 3、选择下载的路径,和
- java计算对数和指数public static void main(String[] args) throws InterruptedEx
- String replace replaceFirst repaceAll区别replace(char oldChar, char newC
- jmap:Java内存映像工具jmap(Memory Map for Java)命令用于生成堆转储快照(一般称为heapdump或dump文
- 一.优先队列的应用优先队列在程序开发中屡见不鲜,比如操作系统在进行进程调度时一种可行的算法是使用优先队列,当一个新的进程被fork()出来后
- 相信大家一定都使用过手机QQ和微信之类的软件,当我们使用时不难发现其界面的切换不仅可以通过点击页标签来实现,还可以通过左右滑动来实现的,耗子
- 本文实例讲述了Android编程使用android-support-design实现MD风格对话框功能。分享给大家供大家参考,具体如下:首先
- 一、首先看下Android开发用到的sdk目录:build-tools保存着一些Android平台相关通用工具,比如adb、和aapt、ai
- Unity3D UGUI Text得分数字增加 代码一、首先在Hierarchy中创建Text,并绑定脚本。using UnityEngin
- Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindServ
- 手头项目需要抓取一个用js渲染出来的网站中的数据。使用常用的httpclient抓回来的页面是没有数据。上网百度了一下,大家推荐的方案是使用
- Java执行hadoop的基本操作实例代码向HDFS上传本地文件public static void uploadInputFile(Str
- 1.概述Spring Boot Admin是一个Web应用程序,用于管理和监视Spring Boot应用程序。每个应用程序都被视为客户端,并
- 1、新建一个Activity,并把各个生命周期打印出来 2、运行Activity,得到如下信息 onCreate--> onStart
- 最近做项目,需要设置用户的生日,所以做这样一个功能。开始发觉自带的DatePicker 很是不好用。上代码:<DatePicker &
- 1、使用org.springframework.beans.BeanUtils.copyProperties方法进行对象之间属性的赋值,避免
- 依赖倒置原则(DIP)定义:高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。问题由来:类A直接依赖类
- 系列文章已完成,目录如下:jdk-logging log4j logback日志系统实现机制原理介绍commons-lo
- 我们在学习接口的时候。能够在里面做一些方法的调用。不过今天所要讲的JDBC,虽然也是连接数据库的一种接口,不过与类接口有着很大的区别,大家要
- 易于理解版package com.zhebie.ternary;public class ternary { public static v