Spring aop 五種通知類型小結(jié)
spring aop 的五種通知類型
- 前置通知 Before advice:在連接點(diǎn)前面執(zhí)行,前置通知不會(huì)影響連接點(diǎn)的執(zhí)行,除非此處拋出異常
- 后置通知 After returning advice:在連接點(diǎn)正常執(zhí)行完成后執(zhí)行,如果連接點(diǎn)拋出異常,則不會(huì)執(zhí)行
- 異常通知 After throwing advice:在連接點(diǎn)拋出異常后執(zhí)行
- 最終通知 After (finally) advice:在連接點(diǎn)執(zhí)行完成后執(zhí)行,不管是正常執(zhí)行完成,還是拋出異常,都會(huì)執(zhí)行返回通知中的內(nèi)容
- 環(huán)繞通知 Around advice:環(huán)繞通知圍繞在連接點(diǎn)前后,能在方法調(diào)用前后自定義一些操作,還需要負(fù)責(zé)決定是繼續(xù)處理 join point (調(diào)用 ProceedingJoinPoint 的 proceed 方法)還是中斷執(zhí)行
五大通知類型中,環(huán)繞通知功能最為強(qiáng)大,因?yàn)榄h(huán)繞通知,可以控制目標(biāo)方法是否執(zhí)行。
如果需要記錄異常信息,使用異常通知。
其他通知,只能做記錄工作,不能做處理,所以執(zhí)行順序其實(shí)對(duì)整個(gè)程序影響不大,沒(méi)有必要太深究。
spring aop 通知類型使用
添加 Aspect 切面類
package com.example.demo.module.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AdviceTest {
// 配置織入點(diǎn)
@Pointcut("execution(public * com.example.demo.module.aspect.Test.*(..))")
public void test(){
}
@Before("test()")
public void doBefore(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName() + "執(zhí)行前置通知---");
}
@AfterReturning("test()")
public void doAfterSuccess(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName() + "執(zhí)行返回通知---");
}
@AfterThrowing("test()")
public void doAfterError(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName() + "執(zhí)行異常通知---");
}
@Around(value = "test()", argNames = "pjp")
public Object doAround(ProceedingJoinPoint pjp) {
Object[] args = pjp.getArgs();
Object result;
try {
// Before
System.out.println(pjp.getSignature().getName() + "環(huán)繞前置通知---");
result = pjp.proceed(args);
// AfterReturning
System.out.println(pjp.getSignature().getName() + "環(huán)繞返回通知---");
}catch (Throwable e){
// AfterThrowing
System.out.println(pjp.getSignature().getName() + "環(huán)繞異常通知---");
throw new RuntimeException(e);
}finally {
// After
System.out.println(pjp.getSignature().getName() + "環(huán)繞最終通知---");
}
return result;
}
@After("test()")
public void doAfter(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName() + "執(zhí)行最終通知---");
}
}
添加測(cè)試方法:
package com.example.demo.module.aspect;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("test")
@RestController
public class Test {
@GetMapping("testMethod")
public void testMethod(){
System.out.println("方法執(zhí)行---");
}
}
運(yùn)行結(jié)果:

測(cè)試異常通知,修改測(cè)試方法:
package com.example.demo.module.aspect;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("test")
@RestController
public class Test {
@GetMapping("testMethod")
public void testMethod(){
int i= 1/0;
System.out.println("方法執(zhí)行---");
}
}
運(yùn)行結(jié)果:

通知執(zhí)行順序
Spring 版本不一樣,通知執(zhí)行順序可能也會(huì)存在差異
@Before、@After、@AfterReturning、@AfterThrowing執(zhí)行順序
- Spring 4.0
正常情況:@Before -> 目標(biāo)方法 -> @After -> @AfterReturning
異常情況:@Before -> 目標(biāo)方法 -> @After -> @AfterThrowing- Spring 5.28
正常情況:@Before -> 目標(biāo)方法 -> @AfterReturning -> @After
異常情況:@Before -> 目標(biāo)方法 -> @AfterThrowing -> @After
@Around的執(zhí)行順序
- Spring 4.0
正常情況:環(huán)繞前置 -> 目標(biāo)方法執(zhí)行 -> 環(huán)繞返回 -> 環(huán)繞最終
異常情況:環(huán)繞前置 -> 目標(biāo)方法執(zhí)行 -> 環(huán)繞異常 -> 環(huán)繞最終- Spring 5.28
正常情況:環(huán)繞前置 -> 目標(biāo)方法執(zhí)行 -> 環(huán)繞返回 -> 環(huán)繞最終
異常情況:環(huán)繞前置 -> 目標(biāo)方法執(zhí)行 -> 環(huán)繞異常 -> 環(huán)繞最終
五大通知執(zhí)行順序
- Spring 4.0
正常情況:環(huán)繞前置 -> @Before -> 目標(biāo)方法執(zhí)行 -> 環(huán)繞返回 -> 環(huán)繞最終 -> @After -> @AfterReturning
異常情況:環(huán)繞前置 -> @Before -> 目標(biāo)方法執(zhí)行 -> 環(huán)繞異常 -> 環(huán)繞最終 -> @After -> @AfterThrowing- Spring 5.28
正常情況:環(huán)繞前置 -> @Before -> 目標(biāo)方法執(zhí)行 -> @AfterReturning -> @After -> 環(huán)繞返回 -> 環(huán)繞最終
異常情況:環(huán)繞前置 -> @Before -> 目標(biāo)方法執(zhí)行 -> @AfterThrowing -> @After -> 環(huán)繞異常 -> 環(huán)繞最終
到此這篇關(guān)于Spring aop 五種通知類型小結(jié)的文章就介紹到這了,更多相關(guān)Spring aop 通知類型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
macOS中搭建Java8開(kāi)發(fā)環(huán)境(基于Intel?x86?64-bit)
這篇文章主要介紹了macOS中搭建Java8開(kāi)發(fā)環(huán)境(基于Intel?x86?64-bit)?的相關(guān)資料,需要的朋友可以參考下2022-12-12
Java創(chuàng)建隨機(jī)數(shù)的四種方式總結(jié)
這篇文章主要介紹了java的四種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2022-07-07
Activiti7與Spring以及Spring Boot整合開(kāi)發(fā)
這篇文章主要介紹了Activiti7與Spring以及Spring Boot整合開(kāi)發(fā),在Activiti中核心類的是ProcessEngine流程引擎,與Spring整合就是讓Spring來(lái)管理ProcessEngine,有感興趣的同學(xué)可以參考閱讀2023-03-03
java實(shí)現(xiàn)發(fā)送手機(jī)短信
這篇文章主要介紹了java實(shí)現(xiàn)發(fā)送手機(jī)短信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
JAVA8妙用Optional解決判斷Null為空的問(wèn)題方法
本文主要介紹了JAVA8妙用Optional解決判斷Null為空的問(wèn)題方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Springboot整合Rabbitmq之Confirm和Return機(jī)制
這篇文章主要介紹了Springboot整合Rabbitmq之Confirm和Return詳解,本篇重點(diǎn)進(jìn)行Confirm?機(jī)制和Return?機(jī)制的實(shí)現(xiàn)和說(shuō)明,通過(guò)實(shí)例代碼相結(jié)合給大家詳細(xì)介紹,對(duì)Springboot整合Rabbitmq相關(guān)知識(shí)感興趣的朋友一起看看吧2022-02-02
Gradle下如何搭建SpringCloud分布式環(huán)境
這篇文章主要介紹了Gradle下如何搭建SpringCloud分布式環(huán)境問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
java.Net.UnknownHostException異常處理問(wèn)題解決
這篇文章主要介紹了java.Net.UnknownHostException異常處理方法,問(wèn)題原因是在系統(tǒng)的?/etc/Hostname中配置了主機(jī)名,而在/etc/hosts文件中沒(méi)有相應(yīng)的配置,本文給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03

