当前位置:网站首页>后置通知的流程分析与功能实现有哪些内容你还记得吗?
后置通知的流程分析与功能实现有哪些内容你还记得吗?
2022-07-29 05:06:00 【是庸医啊】
目录
后置通知
- 后置通知是在连接点完成之后执行的,即连接点返回结果或抛出异常的时候
- 一个切面可以包括一个或者多个通知
// 后置通知: 在目标方法执行后(无论是否发生异常),执行的通知 // 在后置通知中还不能访问目标方法执行的结果 @After("execution(public int com.young.spring.beans.aop.impl.ArithmeticCalculator.*(int,int))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method "+methodName+" ends"); }- 后置通知, 在方法执行之后执行
- 注意,无论方法是否抛出异常,后置通知都会被执行
- 后置通知不能得到目标方法的返回值,返回值需要在返回通知中才能得到
后置通知@AfterReturning的流程分析

后置通知功能的基本实现
服务
业务

切面


测试

后置通知可以改变目标返回值
(首先创建maven项目,加入spring依赖,加入aspectj依赖,创建接口,创建目标类,创建切面,在切面中加入后置通知,修改返回值,运行观察方法的返回值是否被改变。)

创建一个学生类,并且使用构造方法

在接口中写改变这个方法
在SomeSever里面实现这些方法

再在切面方法中改变目标方法

测试

总结:
spring中在后置通知中修改引用类型的返回值会影响最后的返回值。
@AfterReturning后置通知:
后置通知是在目标方法执行后切入切面功能,可以得到目标方法的返回值.如果目标方法的返回值是简单类型(8种基本类型+String)则不可改变.如果目标方法的返回值是引用类型则可以改变。
@Aspect
@Component
public class MyAspect {
/**
* 后置通知的方法的规范
* 1)访问权限是public
* 2)方法没有返回值void
* 3)方法名称自定义
* 4)方法有参数(也可以没有参数,如果目标方法没有返回值,则可以写无参的方法,但一般会写有参,这样可以处理无参可以处理有参),这个切面方法的参数就是目标方法的返回值
* 5)使用@AfterReturning注解表明是后置通知
* 参数:
* value:指定切入点表达式
* returning:指定目标方法的返回值的名称,则名称必须与切面方法的参数名称一致.
*/
@AfterReturning(value = "execution(* com.bjpowernode.s02.*.*(..))",returning = "obj")
public void myAfterReturning(Object obj){
System.out.println("后置通知功能实现..............");
if(obj != null){
if(obj instanceof String){
obj = obj.toString().toUpperCase();
System.out.println("在切面方法中目标方法的返回值:"+obj);
}
if(obj instanceof Student){
Student stu = (Student) obj;
stu.setName("李四");
System.out.println("在切面方法中目标方法的返回值:"+stu);
}
}
}
}
边栏推荐
- Northeast University Data Science Foundation (matlab) - Notes
- 金达威董秘回复:公司看好NMN产品的市场前景,已推出系列产品
- 【无标题】
- Solution | get the relevant information about the current employees' highest salary in each department |
- Simple user-defined authentication interface rules
- C language implementation of three chess
- Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
- Operator operation list of spark
- Sguard64.exe ace guard client exe: frequent disk reading and writing, game jamming, and Solutions
- [untitled]
猜你喜欢

The song of the virtual idol was originally generated in this way!

开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开

【无标题】

使用Jstack、Jconsole和jvisualvm进行死锁分析

The method and detailed code of automatically pop-up and QQ group when players visit the website

How does excel filter out the content you want? Excel table filtering content tutorial

Un7.28: common commands of redis client.

On prepayment of house purchase

Flink+Iceberg环境搭建及生产问题处理

Solution | get the relevant information about the current employees' highest salary in each department |
随机推荐
新产品上市最全推广方案
MySQL time calculation function
P5714 [deep foundation 3. Case 7] obesity
Academic | [latex] super detailed texlive2022+tex studio download installation configuration
【无标题】
使用Jstack、Jconsole和jvisualvm进行死锁分析
那个准时上下班,从不愿意加班加点的人,在我前面升职了...
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!
[untitled]
Northeast University Data Science Foundation (matlab) - Notes
传奇开区网站如何添加流量统计代码
Mysql:the user specified as a definer ('root '@'%) does not exist
Mapper agent development
Torch.nn.crossentropyloss() details
What if the office prompts that the system configuration cannot run?
WPS如何进行快速截屏?WPS快速截屏的方法
Activity workflow table structure learning
How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
Spark的算子操作列表
Network Security Learning - Intranet Security 1