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

SpringAOP操作 ——AspectJ注解

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

SpringAOP操作 ——AspectJ注解

User

@Component
public class User {
    public void add(){
//        int a=1/0;
        System.out.println("add....");
    }
}

UserProxy

@Component
@Aspect
public class UserProxy {

    @Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void before(){
        System.out.println("before...");
    }

    @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("after...");
    }

    @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning...");
    }

    @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing...");
    }

    @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前...");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后...");
    }
}

spring配置文件 




    

    

测试方法 

    @Test
    public void testAopAnno(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        user.add();
    }

 运行结果

 

 在add方法中加入int a=1/0;

 

方法错误,afterReturning不执行;after一定执行。 

总结

正常情况下执行顺序:Around start -> Before ->方法执行-> Around end -> After -> AfterReturning
有异常情况下执行顺序:Around start -> Before -> After -> AfterThrowing 


优先级:有多个增强类对同一个方法进行增强
@Component
@Aspect
@Order(1)
public class PersonProxy {

    @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void pointdemo(){

    }

    @Before(value = "pointdemo()")
    public void before(){
        System.out.println("before2...");
    }

    @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("after2...");
    }

    @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning2...");
    }

    @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing2...");
    }

    @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前2...");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后2..");
    }
}

 设置UserProxy -@Order(3)

结果 

图示https://blog.csdn.net/qq_34412985/article/details/107383520?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164344689516780357222479%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164344689516780357222479&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-107383520.first_rank_v2_pc_rank_v29&utm_term=spring5%E9%80%9A%E7%9F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F&spm=1018.2226.3001.4187

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/720937.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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