基于Spring auto-proxy 和基于 AspectJ pointcuts的实现

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。

目录

概述

Spring auto-proxy

BeanNameAutoProxyCreator : 可以配置需要被进行auto-proxy的bean names列表,它控制的是需要代理的bean列表
InfrastructureAdvisorAutoProxyCreator
DefaultAdvisorAutoProxyCreator : 将对应匹配的advisor,自动添加到spring的bean。它控制的是advisor的匹配,所有的bean都会被自动代理

发现去扩展spring的一些点,是学习spring最快的一种方式,找扩展点的过程是对spring的一个总体把握的过程

AspectJ pointcuts的实现

AspectJ是Eclipse旗下的一个项目。至于它和Spring AOP的关系,不妨可将Spring AOP看成是Spring这个庞大的集成框架为了集成AspectJ而出现的一个模块。

AspectJ: 基于字节码操作(Bytecode Manipulation)
在 Spring 中, 所有的方法都可以认为是 joinpoint, 但是我们并不希望在所有的方法上都添加 Advice, 而 pointcut 的作用就是提供一组规则(使用 AspectJ pointcut expression language 来描述) 来匹配joinpoint, 给满足规则的 joinpoint 添加 Advice.

声明 pointcut

一个 pointcut 的声明由两部分组成:
一个方法签名, 包括方法名和相关参数
一个 pointcut 表达式, 用来指定哪些方法执行是我们感兴趣的(即因此可以织入 advice)

在@AspectJ 风格的 AOP 中, 我们使用一个方法来描述 pointcut, 即:
@Pointcut(“execution(* com.ailianshuo.springaop.sample03….(…))”)
第一个表示匹配任意的方法返回值,…(两个点)表示零个或多个,上面的第一个…表示service包及其子包,第二个表示所有类,第三个*表示所有方法,第二个…表示方法的任意参数个数

/**
     * 定义一个切点然后复用
     * 这个方法必须无返回值.
     *这个方法本身就是 pointcut signature, pointcut 表达式使用@Pointcut 注解指定.
     *上面我们简单地定义了一个 pointcut, 这个 pointcut 所描述的是: 匹配所有在包   *com.ailianshuo.springaop.sample03.Math 下的所有方法的执行.
     */
    @Pointcut("execution(* com.ailianshuo.springaop.sample03.Math.*(..))")   // 切点表达式
    public void pointcutMath(){// 切点前面
    } 

    @Before("pointcutMath()")
    public void before(JoinPoint jp){
        //System.out.println(jp.getSignature().getName());
        System.out.println("----------before advice----------");
    } 
    @After("pointcutMath()")
    public void after_execution(JoinPoint jp){
        System.out.println("----------after execution advice----------");
    }


切点函数:
AspectJ 的切点表达式由标志符(designator)和操作参数组成. 如 “execution( greetTo(…))” 的切点表达式, execution 就是 标志符, 而圆括号里的 greetTo(…) 就是操作参数

常见的AspectJ表达式函数:
execution():满足匹配模式字符串的所有目标类方法的连接点
@annotation():任何标注了指定注解的目标方法链接点
args():目标类方法运行时参数的类型指定连接点
@args():目标类方法参数中是否有指定特定注解的连接点
within():匹配指定的包的所有连接点
target():匹配指定目标类的所有方法
@within():匹配目标对象拥有指定注解的类的所有方法
@target():匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解
this():匹配当前AOP代理对象类型的所有执行方法
最常用的是:execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)切点函数,可以满足多数需求。

annotation –> args –> execution –> this –> within

问题

实现思路分析

相关工具如下:

分析:

小结:

主要讲述了注解配置方式的Mybatis数据库源源码分析原理剖析, 里面有许多不足,请大家指正~

参考资料和推荐阅读

1.链接: 参考资料.
2.链接: 参考资料.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值