博客
关于我
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深度学习 | 基于YOLO11的车体部件检测与分割
    查看>>
    OpenCV与AI深度学习 | 基于YOLOv8的停车对齐检测
    查看>>
    OpenCV与AI深度学习 | 基于机器视觉的磁瓦表面缺陷检测方案
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用YoloV8实例分割识别猪的姿态(含数据集)
    查看>>
    Opencv中KNN背景分割器
    查看>>
    OpenCV中基于已知相机方向的透视变形
    查看>>
    opencv之namedWindow,imshow出现两个窗口
    查看>>
    opencv之模糊处理
    查看>>
    opencv保存图片路径包含中文乱码解决方案
    查看>>
    opencv图像分割2-GMM
    查看>>
    OpenCV学习(13) 细化算法(1)(转)
    查看>>
    OpenCV探索
    查看>>
    opencv笔记(1):图像缩放
    查看>>
    OpenCV(1)读写图像
    查看>>
    OpenCV:概念、历史、应用场景示例、核心模块、安装配置
    查看>>
    Openlayers Source基础及重点内容讲解
    查看>>
    openlayers 入门教程(八):Geoms 篇
    查看>>
    openlayers 入门教程(四):layers 篇
    查看>>
    Openlayers中使用Cluster实现缩放地图时图层聚合与取消聚合
    查看>>
    Openlayers中点击地图获取坐标并输出
    查看>>