当前位置:网站首页>This article takes you to understand the implementation of surround notification @around and final notification @after
This article takes you to understand the implementation of surround notification @around and final notification @after
2022-07-29 05:15:00 【It's a quack】
Catalog
Surrounding the notification @Around
edit Surrounding the notification
Add various notifications to a method
Surrounding the notification @Around
It is by intercepting the target method , Notification of enhancements before and after the target method . It is the most powerful notification , General business uses this notice . It can easily change the return value of the target method .
Execute the process

Code implementation
Interface
Implementation class
Section class
Surrounding the notification
@Aspect
@Component
public class MyAspect {
/**
* Specification of surrounding notification method
* 1) Access rights are public
* 2) The facet method has a return value , This return value is the return value of the target method
* 3) Method name customization
* 4) Method has parameters , This parameter is the target method
* 5) Avoid exceptions Throwable
* 6) Use @Around Annotation declarations are around notifications
* Parameters :
* value: Specify pointcut expression
*/
@Around(value = "execution(* com.bjpowernode.s03.*.*(..))")
public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
// The forward cutting function is realized
System.out.println(" Pre function implementation in surround notification ............");
// Target method call
Object obj = pjp.proceed(pjp.getArgs());
// The back cutting function is realized
System.out.println(" Post function implementation in surround notification ............");
return obj.toString().toUpperCase(); // Changed the return value of the target method
}
}
Final notice @After
Whether or not the target method performs normally , The final notification code will be executed .
Add various notifications to a method
Alias the pointcut expression
Alias the pointcut expression @Pointcut
If multiple facets cut into the same entry point , Aliases can be used to simplify development .
Use @Pointcut annotation , Create an empty method , The name of this method is alias .
@Aspect
@Component
public class MyAspect {
/**
* Specification of the final notification method
* 1) Access rights are public
* 2) Method does not return a value
* 3) Method name customization
* 4) Method has no parameters , If so, it can only be JoinPoint
* 5) Use @After The note indicates that it is the final notice
* Parameters :
* value: Specify pointcut expression
*/
@After(value = "mycut()")
public void myAfter(){
System.out.println(" The function of final notification ........");
}
@Before(value = "mycut()")
public void myBefore(){
System.out.println(" The function of pre notification ........");
}
@AfterReturning(value = "mycut()",returning = "obj")
public void myAfterReturning(Object obj){
System.out.println(" Post notification function ........");
}
@Around(value = "mycut()")
public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(" Surround the function of pre notification in notification ........");
Object obj = pjp.proceed(pjp.getArgs());
System.out.println(" Surround the function of post notification in notification ........");
return obj;
}
@Pointcut(value = "execution(* com.bjpowernode.s04.*.*(..))")
public void mycut(){}
}
test
(1)Before: Enhance before the target method is called ,@Before Just specify the pointcut expression
(2)AfterReturning: Enhance after the target method is completed normally ,@AfterReturning Except after specifying the pointcut expression , You can also specify a return value parameter name returning, Represents the return value of the target method
(3)AfterThrowing: Mainly used to handle unhandled exceptions in the program ,@AfterThrowing Except after specifying the pointcut expression , You can also specify a throwing The return value parameter name of , You can use this parameter name to access the exception object thrown in the target method
(4)After: Enhance after the target method is completed , No matter when the target method is successfully completed [email protected] You can specify a pointcut expression
(5)Around: Surrounding the notification , Enhance before and after the completion of the target method , Surround notification is the most important type of notification , Like business , Logs and so on are all around notifications , Note that the core of programming is a ProceedingJoinPoint
边栏推荐
- Five correlation analysis, one of the most important skills of data analysts
- 缓存穿透、缓存击穿、缓存雪崩以及解决方法
- 传奇开区网站如何添加流量统计代码
- Mysql语句中的函数
- SparkSql批量插入或更新,保存数据到Mysql中
- About realizing page Jump of website in Servlet
- Deadlock to be resolved
- 2021-10-11
- The song of the virtual idol was originally generated in this way!
- Apache POI实现Excel导入读取数据和写入数据并导出
猜你喜欢
TCP three handshakes and four waves
传奇开区网站如何添加流量统计代码
Let you understand several common traffic exposure schemes in kubernetes cluster
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
后置通知的流程分析与功能实现有哪些内容你还记得吗?
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
Apache POI implements excel import, read data, write data and export
Sparksql inserts or updates in batches and saves data to MySQL
MySQL many to many relationship, grouping and splicing to query multiple data to one data
Glory 2023 push, push code ambubk
随机推荐
Let you understand several common traffic exposure schemes in kubernetes cluster
传奇服务端如何添加地图
Google GTEST event mechanism
Mysql语句中的函数
开区网站打开自动播放音乐的添加跟修改教程
2021-11-02
关于thymeleaf的配置与使用
Spark的算子操作列表
2021-10-23
How does WPS take quick screenshots? WPS quick screenshot method
Soft link & hard link
Vivo market API event reporting and docking
Mysql把查询到的结果集按指定顺寻进行排序
Simple user-defined authentication interface rules
The method and detailed code of automatically pop-up and QQ group when players visit the website
P5714 [deep foundation 3. Case 7] obesity
Getting started with solidity
深度学习刷SOTA的一堆trick
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
Connection database time zone setting