栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringAOP执行顺序的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringAOP执行顺序的问题

说明:从网上百度后,可以知道,从Spring 5.2.7版本后,执行的顺序发生的改变,感兴趣的可以看下这篇文章你真的确定Spring AOP的执行顺序吗 https://zhuanlan.zhihu.com/p/266111498

一个切面的情况

两个切面的情况

环境 pom.xml


    4.0.0
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-aop
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.example.demo.DemoApplication
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    


测试代码
package cn.tedu;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


public class MyAopTest {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserService us = context.getBean(UserService.class);
        us.addUser();
    }

}
@Configuration
@ComponentScan("cn.tedu")
@EnableAspectJAutoProxy
class MyConfig{

}
interface UserService{
    void addUser();
}

@Service
class UserServiceImpl implements UserService{

    @Override
    public void addUser() {
        System.out.println("UserServiceImpl.addUser();");
    }
}

@Component
@Aspect
@Order(1)
class MyAop1 {
    @Pointcut("within(cn.tedu.UserServiceImpl)")
    public void pc() {
    }

    @Before("pc()")
    public void Before() {
        System.out.println("aop1.before() 前置通知");
    }

    @AfterReturning("pc()")
    public void afterReturning() {

        System.out.println("aop1.afterRetruning() 返回后通知");
    }

    @After("pc()")
    public void after() {
        System.out.println("aop1.after() 后置通知");
    }

    @AfterThrowing("pc()")
    public void afterThrowing() {
        System.out.println("aop1.afterThrowing() 抛异常后通知");
    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aop1.around_before() 环绕前");
        Object result = joinPoint.proceed();
        System.out.println("aop1.around_after() 环绕后");
        return result;
    }
}

@Component
@Aspect
@Order(2)
class MyAop2 {
    @Pointcut("within(cn.tedu.UserServiceImpl)")
    public void pc() {
    }

    @Before("pc()")
    public void Before() {
        System.out.println("aop2.before() 前置通知");
    }

    @AfterReturning("pc()")
    public void afterReturning() {
        System.out.println("aop2.afterRetruning() 返回后通知");
    }

    @After("pc()")
    public void after() {
        System.out.println("aop2.after() 后置通知");
    }

    @AfterThrowing("pc()")
    public void afterThrowing() {
        System.out.println("aop2.afterThrowing() 抛异常后通知");
    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aop2.around_before() 环绕前");
        Object result = joinPoint.proceed();
        System.out.println("aop2.around_after() 环绕后");
        return result;
    }
}
执行结果
aop1.around_before() 环绕前
aop1.before() 前置通知
aop2.around_before() 环绕前
aop2.before() 前置通知
UserServiceImpl.addUser();
aop2.afterRetruning() 返回后通知
aop2.after() 后置通知
aop2.around_after() 环绕后
aop1.afterRetruning() 返回后通知
aop1.after() 后置通知
aop1.around_after() 环绕后
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号