博客
关于我
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/

    你可能感兴趣的文章
    PHP函数__autoload失效原因(与smarty有关)
    查看>>
    PHP函数判断移动端和PC端
    查看>>
    Springboot基础入门
    查看>>
    php函数性能优化中应注意哪些问题?
    查看>>
    PHP函数操作数字和汉字互转(100以内)
    查看>>
    PHP函数方法
    查看>>
    PHP创建目录mkdir无写入权限的问题解决方案
    查看>>
    PHP删除指定目录下的所有文件和文件夹 | 删除指定文件
    查看>>
    php删除文件夹下面所有文件包括(删除文件夹)不删除文件夹
    查看>>
    React Collapse Pane 项目教程
    查看>>
    php判断ip黑名单程序代码
    查看>>
    php判断复选框是否被选中的方法
    查看>>
    PHP判断指定目录下是否存在文件
    查看>>
    php判断数组是否为空
    查看>>
    PHP判断数组是否有重复值、获取重复值
    查看>>
    springboot基于Web的社区留守儿童管理系统源码毕设+论文
    查看>>
    Springboot基于Redisson实现Redis分布式可重入锁【案例到源码分析】
    查看>>
    PHP利用正则表达式实现手机号码中间4位用星号(*)替换显示
    查看>>
    PHP加密与安全的最佳实践
    查看>>
    PHP加速器eaccelerator导致php-fpm进程卡死原因分析
    查看>>