Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 12016|回复: 0

[原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-4-16 16:56:13 | 显示全部楼层 |阅读模式

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1.通知分类: 

     @Before: 前置通知, 在方法执行之前执行

     @After: 后置通知, 在方法执行之后执行

     @AfterRunning: 返回通知, 在方法返回结果之后执行

     @AfterThrowing: 异常通知, 在方法抛出异常之后

     @Around: 环绕通知, 围绕着方法执行

     

      关于方法签名 看第五点

     

    2.前置通知

        

     

     

     

    3.后置通知:在后置通知中,不能访问目标方法执行的结果

               

     

     

     

    4.返回通知

                 

             

     

    5.异常通知

            

     

    6.环绕通知

          

          

     

     1 package com.jason.spring.aop.impl;  2 
     3 import java.util.Arrays;  4 import java.util.List;  5 
     6 import org.aspectj.lang.JoinPoint;  7 import org.aspectj.lang.ProceedingJoinPoint;  8 import org.aspectj.lang.annotation.After;  9 import org.aspectj.lang.annotation.AfterReturning;  10 import org.aspectj.lang.annotation.AfterThrowing;  11 import org.aspectj.lang.annotation.Around;  12 import org.aspectj.lang.annotation.Aspect;  13 import org.aspectj.lang.annotation.Before;  14 import org.springframework.stereotype.Component;  15 
     16 
     17 //把这个类声明为一个切面  18 //1.需要将该类放入到IOC 容器中
     19 @Component  20 //2.再声明为一个切面
     21 @Aspect  22 public class LoggingAspect {  23     
     24     //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法  25     //作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法  26     //支持通配符  27     //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
     28     @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")  29     public void beforeMethod(JoinPoint joinPoint){  30         String methodName = joinPoint.getSignature().getName();  31         List<Object> args = Arrays.asList(joinPoint.getArgs());  32         System.out.println("The method " + methodName + " begins " + args);  33  }  34     
     35     /**
     36  *  37  * @Author:jason_zhangz@163.com  38  * @Title: afterMethod  39  * @Time:2016年12月1日  40  * @Description: 在方法执行后执行的代码,无论该方法是否出现异常  41  *  42  * @param joinPoint  43      */
     44     @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")  45     public void afterMethod(JoinPoint joinPoint){  46         String methodName = joinPoint.getSignature().getName();  47         List<Object> args = Arrays.asList(joinPoint.getArgs());  48         System.out.println("The method " + methodName + " end " + args);  49  }  50     
     51     /**
     52  *  53  * @Author:jason_zhangz@163.com  54  * @Title: afterReturning  55  * @Time:2016年12月1日  56  * @Description: 在方法正常结束后执行代码,放回通知是可以访问到方法的返回值  57  *  58  * @param joinPoint  59      */
     60     @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")  61     public void afterReturning(JoinPoint joinPoint ,Object result){  62         String methodName = joinPoint.getSignature().getName();  63         System.out.println("The method " + methodName + " end with " + result);  64  }  65     
     66     /**
     67  *  68  * @Author:jason_zhangz@163.com  69  * @Title: afterThrowting  70  * @Time:2016年12月1日  71  * @Description: 在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码  72  *  73  * @param joinPoint  74  * @param ex  75      */
     76     @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")  77     public void afterThrowting(JoinPoint joinPoint, Exception ex){  78         String methodName = joinPoint.getSignature().getName();  79         System.out.println("The method " + methodName + " occurs exceptions " + ex);  80  }  81     
     82     /**
     83  *  84  * @Author:jason_zhangz@163.com  85  * @Title: around  86  * @Time:2016年12月1日  87  * @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数  88  * 环绕通知 类似于 动态代理的全过程  89  * ProceedingJoinPoint:可以决定是否执行目标方法  90  * 环绕通知必须有返回值,返回值即为目标方法的返回值  91  *  92  * @param proceedingJoinPoint  93      */
     94     @Around("execution(* com.jason.spring.aop.impl.*.*(..))")  95     public Object around(ProceedingJoinPoint proceedingJoinPoint){  96         
     97         Object result = null;  98         String methodName = proceedingJoinPoint.getSignature().getName();  99         
    100         //执行目标方法
    101         try { 102             //前置通知
    103             System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs())); 104             
    105             result = proceedingJoinPoint.proceed(); 106             
    107             //后置通知
    108             System.out.println("The method " + methodName + "end with" + result); 109             
    110         } catch (Throwable e) { 111             //异常通知
    112             System.out.println("The method occurs exception : " + e); 113             throw new RuntimeException(); 114  } 115             //后置通知
    116             
    117         System.out.println("The method " + methodName + "end with" + result); 118         
    119         return result; 120         
    121  } 122     
    123     
    124 }

     

     

     

     

     

        

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-5 12:44 , Processed in 0.074377 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表