Spring AOP執(zhí)行先后順序?qū)嵗斀?/h1>
更新時(shí)間:2020年01月22日 15:20:53 作者:panchanggui
這篇文章主要介紹了Spring AOP執(zhí)行先后順序?qū)嵗斀?文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
這篇文章主要介紹了Spring AOP執(zhí)行先后順序?qū)嵗斀?文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
眾所周知,spring聲明式事務(wù)是基于AOP實(shí)現(xiàn)的,那么,如果我們在同一個(gè)方法自定義多個(gè)AOP,我們?nèi)绾沃付ㄋ麄兊膱?zhí)行順序呢?
網(wǎng)上很多答案都是指定order,order越小越是最先執(zhí)行,這種也不能算是錯(cuò),但有些片面。
配置AOP執(zhí)行順序的三種方式:
通過實(shí)現(xiàn)org.springframework.core.Ordered接口
@Component
@Aspect
@Slf4j
public class MessageQueueAopAspect1 implements Ordered{@Override
public int getOrder() {
// TODO Auto-generated method stub
return 2;
}
}
通過注解
@Component
@Aspect
@Slf4j
@Order(1)
public class MessageQueueAopAspect1{
...
}
通過配置文件配置
<aop:config expose-proxy="true">
<aop:aspect ref="aopBean" order="0">
<aop:pointcut id="testPointcut" expression="@annotation(xxx.xxx.xxx.annotation.xxx)"/>
<aop:around pointcut-ref="testPointcut" method="doAround" />
</aop:aspect>
</aop:config>
我們在同一個(gè)方法上加以下兩個(gè)AOP,看看究竟。
@Component
@Aspect
@Slf4j
public class MessageQueueAopAspect1 implements Ordered{
@Resource(name="actionMessageProducer")
private IProducer<MessageQueueInfo> actionProducer;
@Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire1)")
private void pointCutMethod() {
}
//聲明前置通知
@Before("pointCutMethod()")
public void doBefore(JoinPoint point) {
log.info("MessageQueueAopAspect1:doBefore");
return;
}
//聲明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")
public void doAfterReturning(JoinPoint point,Object returnValue) {
log.info("MessageQueueAopAspect1:doAfterReturning");
}
//聲明例外通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e) {
log.info("MessageQueueAopAspect1:doAfterThrowing");
}
//聲明最終通知
@After("pointCutMethod()")
public void doAfter() {
log.info("MessageQueueAopAspect1:doAfter");
}
//聲明環(huán)繞通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
log.info("MessageQueueAopAspect1:doAround-1");
Object obj = pjp.proceed();
log.info("MessageQueueAopAspect1:doAround-2");
return obj;
}
@Override
public int getOrder() {
return 1001;
}
}
@Component
@Aspect
@Slf4j
public class MessageQueueAopAspect2 implements Ordered{
@Resource(name="actionMessageProducer")
private IProducer<MessageQueueInfo> actionProducer;
@Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire2)")
private void pointCutMethod() {
}
//聲明前置通知
@Before("pointCutMethod()")
public void doBefore(JoinPoint point) {
log.info("MessageQueueAopAspect2:doBefore");
return;
}
//聲明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")
public void doAfterReturning(JoinPoint point,Object returnValue) {
log.info("MessageQueueAopAspect2:doAfterReturning");
}
//聲明例外通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e) {
log.info("MessageQueueAopAspect2:doAfterThrowing");
}
//聲明最終通知
@After("pointCutMethod()")
public void doAfter() {
log.info("MessageQueueAopAspect2:doAfter");
}
//聲明環(huán)繞通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
log.info("MessageQueueAopAspect2:doAround-1");
Object obj = pjp.proceed();
log.info("MessageQueueAopAspect2:doAround-2");
return obj;
}
@Override
public int getOrder() {
return 1002;
}
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
@MessageQueueRequire1
@MessageQueueRequire2
public PnrPaymentErrCode bidLoan(String id){
...
}
看看執(zhí)行結(jié)果:

從上面的測試我們看到,確實(shí)是order越小越是最先執(zhí)行,但更重要的是最先執(zhí)行的最后結(jié)束。
這個(gè)不難理解,Spring AOP就是面向切面編程,什么是切面,畫一個(gè)圖來理解下:

由此得出:spring aop就是一個(gè)同心圓,要執(zhí)行的方法為圓心,最外層的order最小。從最外層按照AOP1、AOP2的順序依次執(zhí)行doAround方法,doBefore方法。然后執(zhí)行method方法,最后按照AOP2、AOP1的順序依次執(zhí)行doAfter、doAfterReturn方法。也就是說對多個(gè)AOP來說,先before的,一定后after。
如果我們要在同一個(gè)方法事務(wù)提交后執(zhí)行自己的AOP,那么把事務(wù)的AOP order設(shè)置為2,自己的AOP order設(shè)置為1,然后在doAfterReturn里邊處理自己的業(yè)務(wù)邏輯。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
詳解Java中finally和return的執(zhí)行順序
try-catch-finally是一種針對程序運(yùn)行時(shí)出錯(cuò)的響應(yīng)手段,對于一些可以預(yù)料到的出錯(cuò)類型,在發(fā)生時(shí)對其進(jìn)行報(bào)告和補(bǔ)救,這篇文章主要介紹了Java中finally和return的執(zhí)行順序,需要的朋友可以參考下 2024-01-01
-
Spring @Bean注解的使用場景與案例實(shí)現(xiàn)
隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用 2023-03-03
-
MyBatis-Plus如何通過注解使用TypeHandler
這篇文章主要介紹了MyBatis-Plus如何通過注解使用TypeHandler,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教 2022-01-01
-
MyBatis-Plus中實(shí)現(xiàn)自定義復(fù)雜排序邏輯的詳細(xì)步驟
這篇文章主要介紹了MyBatis-Plus中實(shí)現(xiàn)自定義復(fù)雜排序邏輯,通過使用MyBatis-Plus的QueryWrapper和SQL原始片段,我們可以靈活地實(shí)現(xiàn)復(fù)雜的數(shù)據(jù)排序邏輯,這種方法尤其適用于需要對數(shù)據(jù)進(jìn)行特定規(guī)則排序的場景,需要的朋友可以參考下 2024-07-07
-
在controller中如何設(shè)置接收參數(shù)的默認(rèn)值
這篇文章主要介紹了在controller中如何設(shè)置接收參數(shù)的默認(rèn)值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教 2022-03-03
-
Java編程實(shí)現(xiàn)NBA賽事接口調(diào)用實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)NBA賽事接口調(diào)用實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。 2017-11-11
最新評論
這篇文章主要介紹了Spring AOP執(zhí)行先后順序?qū)嵗斀?文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
眾所周知,spring聲明式事務(wù)是基于AOP實(shí)現(xiàn)的,那么,如果我們在同一個(gè)方法自定義多個(gè)AOP,我們?nèi)绾沃付ㄋ麄兊膱?zhí)行順序呢?
網(wǎng)上很多答案都是指定order,order越小越是最先執(zhí)行,這種也不能算是錯(cuò),但有些片面。
配置AOP執(zhí)行順序的三種方式:
通過實(shí)現(xiàn)org.springframework.core.Ordered接口
@Component
@Aspect
@Slf4j
public class MessageQueueAopAspect1 implements Ordered{@Override
public int getOrder() {
// TODO Auto-generated method stub
return 2;
}
}
通過注解
@Component
@Aspect
@Slf4j
@Order(1)
public class MessageQueueAopAspect1{
...
}
通過配置文件配置
<aop:config expose-proxy="true">
<aop:aspect ref="aopBean" order="0">
<aop:pointcut id="testPointcut" expression="@annotation(xxx.xxx.xxx.annotation.xxx)"/>
<aop:around pointcut-ref="testPointcut" method="doAround" />
</aop:aspect>
</aop:config>
我們在同一個(gè)方法上加以下兩個(gè)AOP,看看究竟。
@Component
@Aspect
@Slf4j
public class MessageQueueAopAspect1 implements Ordered{
@Resource(name="actionMessageProducer")
private IProducer<MessageQueueInfo> actionProducer;
@Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire1)")
private void pointCutMethod() {
}
//聲明前置通知
@Before("pointCutMethod()")
public void doBefore(JoinPoint point) {
log.info("MessageQueueAopAspect1:doBefore");
return;
}
//聲明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")
public void doAfterReturning(JoinPoint point,Object returnValue) {
log.info("MessageQueueAopAspect1:doAfterReturning");
}
//聲明例外通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e) {
log.info("MessageQueueAopAspect1:doAfterThrowing");
}
//聲明最終通知
@After("pointCutMethod()")
public void doAfter() {
log.info("MessageQueueAopAspect1:doAfter");
}
//聲明環(huán)繞通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
log.info("MessageQueueAopAspect1:doAround-1");
Object obj = pjp.proceed();
log.info("MessageQueueAopAspect1:doAround-2");
return obj;
}
@Override
public int getOrder() {
return 1001;
}
}
@Component
@Aspect
@Slf4j
public class MessageQueueAopAspect2 implements Ordered{
@Resource(name="actionMessageProducer")
private IProducer<MessageQueueInfo> actionProducer;
@Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire2)")
private void pointCutMethod() {
}
//聲明前置通知
@Before("pointCutMethod()")
public void doBefore(JoinPoint point) {
log.info("MessageQueueAopAspect2:doBefore");
return;
}
//聲明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")
public void doAfterReturning(JoinPoint point,Object returnValue) {
log.info("MessageQueueAopAspect2:doAfterReturning");
}
//聲明例外通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e) {
log.info("MessageQueueAopAspect2:doAfterThrowing");
}
//聲明最終通知
@After("pointCutMethod()")
public void doAfter() {
log.info("MessageQueueAopAspect2:doAfter");
}
//聲明環(huán)繞通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
log.info("MessageQueueAopAspect2:doAround-1");
Object obj = pjp.proceed();
log.info("MessageQueueAopAspect2:doAround-2");
return obj;
}
@Override
public int getOrder() {
return 1002;
}
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
@MessageQueueRequire1
@MessageQueueRequire2
public PnrPaymentErrCode bidLoan(String id){
...
}
看看執(zhí)行結(jié)果:

從上面的測試我們看到,確實(shí)是order越小越是最先執(zhí)行,但更重要的是最先執(zhí)行的最后結(jié)束。
這個(gè)不難理解,Spring AOP就是面向切面編程,什么是切面,畫一個(gè)圖來理解下:

由此得出:spring aop就是一個(gè)同心圓,要執(zhí)行的方法為圓心,最外層的order最小。從最外層按照AOP1、AOP2的順序依次執(zhí)行doAround方法,doBefore方法。然后執(zhí)行method方法,最后按照AOP2、AOP1的順序依次執(zhí)行doAfter、doAfterReturn方法。也就是說對多個(gè)AOP來說,先before的,一定后after。
如果我們要在同一個(gè)方法事務(wù)提交后執(zhí)行自己的AOP,那么把事務(wù)的AOP order設(shè)置為2,自己的AOP order設(shè)置為1,然后在doAfterReturn里邊處理自己的業(yè)務(wù)邏輯。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java中finally和return的執(zhí)行順序
try-catch-finally是一種針對程序運(yùn)行時(shí)出錯(cuò)的響應(yīng)手段,對于一些可以預(yù)料到的出錯(cuò)類型,在發(fā)生時(shí)對其進(jìn)行報(bào)告和補(bǔ)救,這篇文章主要介紹了Java中finally和return的執(zhí)行順序,需要的朋友可以參考下2024-01-01
Spring @Bean注解的使用場景與案例實(shí)現(xiàn)
隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用2023-03-03
MyBatis-Plus如何通過注解使用TypeHandler
這篇文章主要介紹了MyBatis-Plus如何通過注解使用TypeHandler,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
MyBatis-Plus中實(shí)現(xiàn)自定義復(fù)雜排序邏輯的詳細(xì)步驟
這篇文章主要介紹了MyBatis-Plus中實(shí)現(xiàn)自定義復(fù)雜排序邏輯,通過使用MyBatis-Plus的QueryWrapper和SQL原始片段,我們可以靈活地實(shí)現(xiàn)復(fù)雜的數(shù)據(jù)排序邏輯,這種方法尤其適用于需要對數(shù)據(jù)進(jìn)行特定規(guī)則排序的場景,需要的朋友可以參考下2024-07-07
在controller中如何設(shè)置接收參數(shù)的默認(rèn)值
這篇文章主要介紹了在controller中如何設(shè)置接收參數(shù)的默認(rèn)值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java編程實(shí)現(xiàn)NBA賽事接口調(diào)用實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)NBA賽事接口調(diào)用實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

