Spring AOP執(zhí)行先后順序?qū)嵗斀?/h1>
更新時間:2020年01月22日 15:20:53 作者:panchanggui
這篇文章主要介紹了Spring AOP執(zhí)行先后順序?qū)嵗斀?文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了Spring AOP執(zhí)行先后順序?qū)嵗斀?文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
眾所周知,spring聲明式事務是基于AOP實現(xiàn)的,那么,如果我們在同一個方法自定義多個AOP,我們?nèi)绾沃付ㄋ麄兊膱?zhí)行順序呢?
網(wǎng)上很多答案都是指定order,order越小越是最先執(zhí)行,這種也不能算是錯,但有些片面。
配置AOP執(zhí)行順序的三種方式:
通過實現(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>
我們在同一個方法上加以下兩個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í)行結果:

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

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

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

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

