博客
关于我
Day125.SpringAOP细节 -Spring
阅读量:330 次
发布时间:2019-03-04

本文共 3246 字,大约阅读时间需要 10 分钟。

AOP??

????????

??????????????????AOP??????????????????????????????

?????????

???????????????????????????????????????????????????

???????????

??????????????

execution([?????] [?????] [????/???] [???]([????]))
  • *?????????????????
  • ..???????????????
  • .??????????

???

execution(* com.atguigu.spring.ArithmeticCalculator.*(..))

??ArithmeticCalculator???????????

?????????

????????????@Pointcut???????????????????

?????????

1?IOC???????????

?Spring??AOP???????????IOC????????????????????????

???

//??Calculator beanCalculator calculator = ioc.getBean(Calculator.class);System.out.println(calculator.getClass()); //??????//??Bean????Calculator calculator1 = ioc.getBean("myMathCalculator");System.out.println(calculator1.getClass()); //??????

2??????????????

???????????????????

  • *????????
  • ..???????????????

???

//??????execution(* com.achang.inter.impl.MyMathCalculator.*(..))

3??????????

????????????????

  • @Before??????
  • @After??????
  • @AfterReturning??????
  • @AfterThrowing??????

4?JoinPoint???????????

JoinPoint????????????????????

???

@After("execution(public int com.achang.inter.impl.MyMathCalculator.*(int,int))")public static void logEnd(JoinPoint joinPoint) {    String methodName = joinPoint.getSignature().getName();    System.out.println("?" + methodName + "?????????");}

5???throwing?returning????

??throwing?returning???????????????????

???

@AfterThrowing(value = "execution(public int com.achang.inter.impl.MyMathCalculator.*(int,int))", throwing = "e")public static void logException(JoinPoint joinPoint, Exception e) {    String methodName = joinPoint.getSignature().getName();    System.out.println("?" + methodName + "?????????????" + e);}@AfterReturning(value = "execution(public int com.achang.inter.impl.MyMathCalculator.*(int,int))", returning = "result")public static void logReturn(JoinPoint joinPoint, Object result) {    String methodName = joinPoint.getSignature().getName();    System.out.println("?" + methodName + "??????????????" + result);}

6?Spring????????

  • ??????????????????????
  • ??????????????????Spring???????

7?????

????????????????????????

???

@Around("OniMyPoint()")public Object myAround(ProceedingJoinPoint pjp) throws Throwable {    Object[] args = pjp.getArgs();    String methodName = pjp.getSignature().getName();        try {        //????        System.out.println("?????????" + methodName + "?????");        //??????        Object proceed = pjp.proceed(args);        //????        System.out.println("?????????" + methodName + "????????????" + proceed + "?");    } catch (Exception e) {        //????        System.out.println("?????????" + methodName + "???????????" + e + "?");        throw new RuntimeException(e);    } finally {        //????        System.out.println("?????????" + methodName + "?????");    }    return proceed;}

8????????

??@Order???????????????????????

???

@Order(1)public class LogUtils {    //...}

??AOP??????????

?????AOP??

  • ??????????IOC???
  • ??@Aspect????????
  • ?????????????????
  • ?????????????AOP???
  • ???XML??????

    ??XML??????????

    • ???????????????????
    • XML????????????????
    • ?????????XML????????????

    转载地址:http://edoq.baihongyu.com/

    你可能感兴趣的文章
    OpenCV与AI深度学习 | 实战 | OpenCV传统方法实现密集圆形分割与计数(详细步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | OpenCV实现扫描文本矫正应用与实现详解(附源码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用YOLOv8 Pose实现瑜伽姿势识别
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用YoloV8实例分割识别猪的姿态(含数据集)
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用姿态估计算法构建简单的健身训练辅助应用程序
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于YoloV5和Mask RCNN实现汽车表面划痕检测(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于YOLOv9+SAM实现动态目标检测和分割(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于YOLOv9和OpenCV实现车辆跟踪计数(步骤 + 源码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 文本图片去水印--同时保持文本原始色彩(附源码)
    查看>>
    OpenCV与AI深度学习 | 实战—使用YOLOv8图像分割实现路面坑洞检测(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战篇——基于YOLOv8和OpenCV实现车速检测(详细步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战|OpenCV实时弯道检测(详细步骤+源码)
    查看>>
    OpenCV与AI深度学习 | 实践教程|旋转目标检测模型-TensorRT 部署(C++)
    查看>>
    OpenCV与AI深度学习 | 工业缺陷检测中数据标注需要注意的几个事项
    查看>>
    OpenCV与AI深度学习 | 干货 | 深度学习模型训练和部署的基本步骤
    查看>>
    OpenCV与AI深度学习 | 手把手教你用Python和OpenCV搭建一个半自动标注工具(详细步骤 + 源码)
    查看>>
    OpenCV与AI深度学习 | 深度学习检测小目标常用方法
    查看>>
    OpenCV与AI深度学习 | 超越YOLOv10/11、RT-DETRv2/3!中科大D-FINE重新定义边界框回归任务
    查看>>
    OpenCV与AI深度学习 | 高效开源的OCR工具:Surya-OCR介绍与使用
    查看>>
    Opencv中KNN背景分割器
    查看>>