当前位置:网站首页>一文带你搞懂环绕通知@Around与最终通知@After的实现
一文带你搞懂环绕通知@Around与最终通知@After的实现
2022-07-29 05:06:00 【是庸医啊】
目录
环绕通知@Around
它是通过拦截目标方法的方式 ,在目标方法前后增强功能的通知.它是功能最强大的通知,一般事务使用此通知.它可以轻易的改变目标方法的返回值。
执行流程

代码实现
接口

实现类

切面类
环绕通知

@Aspect
@Component
public class MyAspect {
/**
* 环绕通知方法的规范
* 1)访问权限是public
* 2)切面方法有返回值,此返回值就是目标方法的返回值
* 3)方法名称自定义
* 4)方法有参数,此参数就是目标方法
* 5)回避异常Throwable
* 6)使用@Around注解声明是环绕通知
* 参数:
* value:指定切入点表达式
*/
@Around(value = "execution(* com.bjpowernode.s03.*.*(..))")
public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
//前切功能实现
System.out.println("环绕通知中的前置功能实现............");
//目标方法调用
Object obj = pjp.proceed(pjp.getArgs());
//后切功能实现
System.out.println("环绕通知中的后置功能实现............");
return obj.toString().toUpperCase(); //改变了目标方法的返回值
}
}
最终通知@After
无论目标方法是否正常执行,最终通知的代码都会被执行。

为一个方法添加各种通知



给切入点表达式起别名
给切入点表达式起别名@Pointcut
如果多个切面切入到同一个切入点,可以使用别名简化开发。
使用@Pointcut注解,创建一个空方法,此方法的名称就是别名。

@Aspect
@Component
public class MyAspect {
/**
* 最终通知方法的规范
* 1)访问权限是public
* 2)方法没有返回值
* 3)方法名称自定义
* 4)方法没有参数,如果有也只能是JoinPoint
* 5)使用@After注解表明是最终通知
* 参数:
* value:指定切入点表达式
*/
@After(value = "mycut()")
public void myAfter(){
System.out.println("最终通知的功能........");
}
@Before(value = "mycut()")
public void myBefore(){
System.out.println("前置通知的功能........");
}
@AfterReturning(value = "mycut()",returning = "obj")
public void myAfterReturning(Object obj){
System.out.println("后置通知的功能........");
}
@Around(value = "mycut()")
public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕通知中的前置通知的功能........");
Object obj = pjp.proceed(pjp.getArgs());
System.out.println("环绕通知中的后置通知的功能........");
return obj;
}
@Pointcut(value = "execution(* com.bjpowernode.s04.*.*(..))")
public void mycut(){}
}测试
(1)Before:在目标方法被调用之前做增强处理,@Before只需要指定切入点表达式即可
(2)AfterReturning:在目标方法正常完成后做增强,@AfterReturning除了指定切入点表达式后,还可以指定一个返回值形参名returning,代表目标方法的返回值
(3)AfterThrowing:主要用来处理程序中未处理的异常,@AfterThrowing除了指定切入点表达式后,还可以指定一个throwing的返回值形参名,可以通过该形参名来访问目标方法中所抛出的异常对象
(4)After:在目标方法完成之后做增强,无论目标方法时候成功完成。@After可以指定一个切入点表达式
(5)Around:环绕通知,在目标方法完成前后做增强处理,环绕通知是最重要的通知类型,像事务,日志等都是环绕通知,注意编程中核心是一个ProceedingJoinPoint
边栏推荐
- excel怎么设置行高和列宽?excel设置行高和列宽的方法
- The method and detailed code of automatically pop-up and QQ group when players visit the website
- Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
- Climbing the pit of traffic flow prediction (II): the simplest LSTM predicts traffic flow using tensorflow2
- Flink+iceberg environment construction and production problem handling
- Word如何查看文档修改痕迹?Word查看文档修改痕迹的方法
- Diagram of odoo development tutorial
- AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
- The song of the virtual idol was originally generated in this way!
- sql日志
猜你喜欢

What are the core features of the digital transformation of state-owned construction enterprises?

Learn matlab to draw geographical map, line scatter bubble density map

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

How to monitor micro web services

Solution to the fourth game of 2022 Hangzhou Electric Multi school league

电脑无法打开excel表格怎么办?excel打不开的解决方法

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

【无标题】

MySQL定时调用预置函数完成数据更新

Solution | get the relevant information about the current employees' highest salary in each department |
随机推荐
Raspberry pie 4B + Intel neural computing stick (stick2) +yolov5 feasibility study report
Introduction of JDBC preparestatement+ database connection pool
Activity workflow table structure learning
Big silent event Google browser has no title
The most comprehensive promotion plan for the launch of new products
sql日志
MySQL regularly calls preset functions to complete data update
电脑无法打开excel表格怎么办?excel打不开的解决方法
Numpy basic learning
Un7.28: common commands of redis client.
JS daily question (11)
Operator operation list of spark
后置通知的流程分析与功能实现有哪些内容你还记得吗?
P2181 diagonal
Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
Simple user-defined authentication interface rules
关于thymeleaf的配置与使用
JS daily question (12)
开区网站打开自动播放音乐的添加跟修改教程
office2010每次打开都要配置进度怎么解决?

