Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序介紹
Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序
Spring AOP有多少個(gè)通知
- ①前置通知(Before):在連接點(diǎn)執(zhí)行前執(zhí)行該通知
- ②正常返回通知(AfterReturning):在連接點(diǎn)正常執(zhí)行完后執(zhí)行該通知,若目標(biāo)方法執(zhí)行異常則不會(huì)執(zhí)行該通知
- ③異常通知(AfterThrowing):在連接點(diǎn)執(zhí)行拋出異常時(shí)執(zhí)行該通知
- ④后置通知(after/finally):在連接點(diǎn)執(zhí)行完成后(不管成功、失敗、異常)都會(huì)執(zhí)行該通知
- ⑤環(huán)繞通知(Around):圍繞在連接點(diǎn)前后
Spring AOP通知的執(zhí)行順序
- ①環(huán)繞通知:@Around
- ②前置通知:@Before
- ③執(zhí)行連接點(diǎn)方法
- ④環(huán)繞通知:@Around
- ⑤后置通知:@After
- ⑥正常返回通知:@AfterReturning,如果發(fā)生異常那么就是異常通知@AfterThrowing

SpringAOP簡(jiǎn)單案例
本文是一個(gè)老師在學(xué)校給學(xué)生上課的簡(jiǎn)單案例,介紹了AOP的五個(gè)通知的使用,以及通知的執(zhí)行順序。通過自定義注解來充當(dāng)切入點(diǎn),獲取注解的類型分別對(duì)不同的老師做對(duì)應(yīng)的業(yè)務(wù)處理。
代碼中的消息響應(yīng)體(Result)大家可以自定義類型。
AOP的五大通知
- 前置通知:Before
- 環(huán)繞通知:Around
- 后置通知:After
- 后置返回通知:AfterReturning
- 后置異常通知:AfterThrowing
執(zhí)行順序如下圖所示:

AOP的使用方式
1.創(chuàng)建一個(gè)課題實(shí)體對(duì)象
package com.cloud.industryapi.test;
import lombok.Data;
/**
* 課題實(shí)體
* @date 2022/3/25 16:26
*/
@Data
public class ArticleEntity {
/**
* PK
*/
private Integer id;
/**
* 課題
*/
private String title;
/**
* 內(nèi)容
*/
private String content;
}2.定義一個(gè)切入點(diǎn),這里以自定義注解的方式實(shí)現(xiàn)
package com.cloud.industryapi.test;
import java.lang.annotation.*;
/**
* 切點(diǎn)標(biāo)識(shí)
* @author
* @date 2022/3/25 13:09
*/
@Target({ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PointcutId {
String type() default "";
}3.聲明要織入的切面
package com.cloud.industryapi.test;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 切面
*/
@Slf4j
@Aspect
@Component
public class ArticleAspect {
/**
* 定義切入點(diǎn)
*/
@Pointcut("@annotation(com.cloud.industryapi.test.PointcutId)")
public void pointcut(){
}
/**
* 環(huán)繞通知
* @param joinPoint
* @param id
* @return
* @throws Throwable
*/
@Around("pointcut() && @annotation(id)")
public Object around(ProceedingJoinPoint joinPoint, PointcutId id) throws Throwable {
log.info("-------------around start-------------");
Object[] objects = joinPoint.getArgs();
switch (id.type()){
case "language":
ArticleEntity language = (ArticleEntity) objects[0];
log.info("-------------語(yǔ)文老師課前備課,課題:{}-------------",language.getTitle());
break;
case "mathematics":
log.info("-------------數(shù)學(xué)老師課前備課-------------");
break;
default:
throw new RuntimeException("類型非法");
}
//joinPoint.proceed()
Object s = joinPoint.proceed();
log.info("-------------叮鈴鈴鈴鈴...放學(xué)了-------------");
log.info("-------------around end-------------");
return s;
}
/**
* 前置通知
* @param joinPoint
* @param id
*/
@Before("pointcut() && @annotation(id)")
public void before(JoinPoint joinPoint,PointcutId id){
log.info("-------------before start-------------");
log.info("-------------學(xué)生進(jìn)入教室,準(zhǔn)備上課-------------");
log.info("-------------before end-------------");
}
/**
* 后置通知
* @param joinPoint
* @param id
*/
@After("pointcut() && @annotation(id)")
public void after(JoinPoint joinPoint,PointcutId id){
log.info("-------------after start-------------");
log.info("-------------學(xué)校廣播:老師們,同學(xué)們,中午好,今天學(xué)校食堂免費(fèi)為你們準(zhǔn)備了燒雞,人手一雞-------------");
log.info("-------------after end-------------");
}
/**
* 后置返回通知
* @param joinPoint
* @param id
*/
@AfterReturning("pointcut() && @annotation(id)")
public void afterReturn(JoinPoint joinPoint, PointcutId id){
log.info("-------------AfterReturning-------------");
log.info("-------------老師們同學(xué)們拿著燒雞回家了-------------");
log.info("-------------學(xué)校關(guān)閉了大門-------------");
log.info("-------------AfterReturning-------------");
}
/**
* 后置異常通知
* @param joinPoint
* @param id
*/
@AfterThrowing("pointcut() && @annotation(id)")
public void afterThrow(JoinPoint joinPoint,PointcutId id){
log.info("-------------AfterThrowing-------------");
log.info("-------------完蛋,小明同學(xué)迷路了。。。-------------");
log.info("-------------AfterThrowing-------------");
}
}注意:ProceedingJoinPoint的proceed()方法相當(dāng)于前置通知和后置通知的分水嶺。
說明:ProceedingJoinPoint的proceed()方法在執(zhí)行前用來做一些
- 例如:讀取日志 ,然后執(zhí)行目標(biāo)方法。ProceedingJoinPoint的proceed()方法執(zhí)行后 ,用來做一些
- 例如:寫入日志\color{#0000FF}{說明:ProceedingJoinPoint的proceed()方法在執(zhí)行前用來做一些
- 例如:讀取日志,然后執(zhí)行目標(biāo)方法。ProceedingJoinPoint的proceed()方法執(zhí)行后,用來做一些
- 例如:寫入日志}說明:ProceedingJoinPoint的proceed()方法在執(zhí)行前用來做一些
- 例如:讀取日志,然后執(zhí)行目標(biāo)方法。ProceedingJoinPoint的proceed()方法執(zhí)行后,用來做一些
- 例如:寫入日志
4.編寫控制器
package com.cloud.industryapi.test;
import com.cloud.common.kit.Result;
import com.cloud.common.page.FrontPagination;
import com.cloud.industry.dto.ExclusiveJumpConfigDto;
import com.cloud.industry.facede.ExclusiveJumpConfigFacede;
import com.cloud.industry.qo.ExclusiveJumpConfigQo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 文章 - 控制器
*/
@Slf4j
@RestController
@RequestMapping("/aop/test")
public class ArticleController {
/**
* 后置異常通知
*/
@PointcutId
@RequestMapping("/afterThrow")
public void afterThrow(){
log.info("-------------------進(jìn)入了目標(biāo)方法-------------------");
System.out.println(2/0);
}
/**
* 語(yǔ)文課
*
* @param ae
* @return
*/
@PointcutId(type = "language")
@PostMapping("/language")
public Result language(@RequestBody ArticleEntity ae) {
log.info("-------------目標(biāo)方法開始執(zhí)行-------------");
log.info("-------------語(yǔ)文老師進(jìn)入教室,開始講課《"+ae.getTitle()+"》-------------");
System.out.printf("%s","深藍(lán)的天空中掛著一輪金黃的圓月,下面是海邊的沙地,都種著一望無(wú)際的碧綠的西瓜。\n" +
"其間有一個(gè)十一二歲的少年,項(xiàng)帶銀圈,手捏一柄鋼叉,向一匹猹盡力的刺去。\n" +
"那猹卻將身一扭,反從他的胯下逃走了。\n");
log.info("-------------目標(biāo)方法結(jié)束執(zhí)行-------------");
return Result.success("執(zhí)行結(jié)束");
}
/**
* 數(shù)學(xué)課
*
* @param ae
* @return
*/
@PointcutId(type = "mathematics")
@PostMapping("/mathematics")
public Result mathematics(@RequestBody ArticleEntity ae) {
log.info("-------------目標(biāo)方法開始執(zhí)行-------------");
log.info("-------------數(shù)學(xué)老師進(jìn)入教室-------------");
log.info("-------------“同學(xué)們,今天這節(jié)數(shù)學(xué)課,由我代上”-------------");
log.info("-------------“起立”-------------");
log.info("-------------”體~育~老~師~好~~“-------------");
log.info("-------------目標(biāo)方法結(jié)束執(zhí)行-------------");
return Result.success("執(zhí)行結(jié)束");
}
}5.請(qǐng)求控制器

最后是請(qǐng)求的響應(yīng)

完成
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Mybatis遇到的there is no getter異常
這篇文章主要介紹了使用Mybatis遇到的there is no getter異常,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
SpringBoot自動(dòng)裝配原理詳細(xì)解析
這篇文章主要介紹了SpringBoot自動(dòng)裝配原理詳細(xì)解析,一個(gè)對(duì)象交給Spring來管理的三種方式 @Bean @Compoment @Import,2024-01-01
@Bean主要在@Configuration中,通過方法進(jìn)行注入相關(guān)的Bean,@Compoent與@Service歸為一類,在類上加注入對(duì)應(yīng)的類,需要的朋友可以參考下
Java實(shí)現(xiàn)快速排序算法(Quicktsort)
這篇文章主要介紹了Java實(shí)現(xiàn)快速排序算法(Quicktsort),有需要的朋友可以參考一下2013-12-12
如何動(dòng)態(tài)改變Retrofit的base url和rest版本詳解
這篇文章主要給大家介紹了關(guān)于如何動(dòng)態(tài)改變Retrofit的base url和rest版本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Springboot如何實(shí)現(xiàn)Web系統(tǒng)License授權(quán)認(rèn)證
這篇文章主要介紹了Springboot如何實(shí)現(xiàn)Web系統(tǒng)License授權(quán)認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Spring cloud restTemplate 傳遞復(fù)雜參數(shù)的方式(多個(gè)對(duì)象)
這篇文章主要介紹了Spring cloud restTemplate 傳遞復(fù)雜參數(shù)的方式(多個(gè)對(duì)象),需要的朋友可以參考下2018-05-05
SpringBoot3.4配置校驗(yàn)新特性的用法詳解
Spring Boot 3.4 對(duì)配置校驗(yàn)支持進(jìn)行了全面升級(jí),這篇文章為大家詳細(xì)介紹了一下它們的具體使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-04-04
java ThreadLocal線程局部變量常用方法使用場(chǎng)景示例詳解
這篇文章主要介紹了為大家java ThreadLocal線程局部變量常用方法使用場(chǎng)景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

