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

    你可能感兴趣的文章
    NFS共享文件系统搭建
    查看>>
    ng 指令的自定义、使用
    查看>>
    nginx + etcd 动态负载均衡实践(二)—— 组件安装
    查看>>
    Nginx + uWSGI + Flask + Vhost
    查看>>
    Nginx Location配置总结
    查看>>
    Nginx 动静分离与负载均衡的实现
    查看>>
    Nginx 反向代理解决跨域问题
    查看>>
    Nginx 反向代理配置去除前缀
    查看>>
    nginx 后端获取真实ip
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    nginx 常用配置记录
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx的Rewrite正则表达式,匹配非某单词
    查看>>
    Nginx的使用总结(一)
    查看>>