Spring AOP
与AspectJ
的关系
两者都可以用来实现动态代理。不同的是:
-
AspectJ
基于asm
做字节码替换来实现AOP,可以在 类编译期 / 类加载期 织入切面。功能更强大,但是无论是从实现还是从使用上来说也更复杂。 -
Spring AOP
基于JDK
动态代理和CGLIB
动态代理实现AOP,因此只能在 运行期 织入切面,但是切点表达式使用了AspectJ
。要开启AspectJ
表达式的支持,需要引入aspectjweaver
包。 - 由于
Spring AOP
基于动态代理实现,因此只能增强方法,且只能增强能被覆写(@Override
)的方法。而AspectJ
不光能增强private
的方法,还能增强字段、构造方法。
实际应用中,Spring AOP能满足绝大部分需求,需要使用
AspectJ
的场景也比较少。而且使用AspectJ
引入的复杂度可能会得不偿失。
Spring AOP
支持的AspectJ
表达式
AspectJ
支持的表达式类型可以通过类org.aspectj.weaver.tools.PointcutPrimitive
查看,而Spring AOP并没有支持所有AspectJ
表达式类型,而是选择了其中最实用的一些进行了支持。具体可以查看org.springframework.aop.aspectj.AspectJExpressionPointcut
。
- Spring AOP只支持bean的方法上的连接点,因此这些切点在使用Spring AOP时都是Spring Bean的方法的执行。
- 由于Spring的AOP框架基于代理的性质,因此根据定义,不会拦截目标对象内的调用(例如a方法中调用当前类中的b方法,只会拦截一次)。对于JDK代理,只能拦截代理上的公共接口方法调用。使用CGLIB,将拦截代理上的公共方法和受保护的方法调用(必要时甚至包可见的方法)。
Spring AOP支持的AspectJ
表达式概览:
-
execution
: 匹配方法执行的切入点。Spring AOP主要使用的切点标识符。 -
within
: 限制匹配在特定类型内的连接点。(给定class的所有方法) -
this
: 限制匹配是给定类型的实例的bean引用(Spring AOP proxy)的连接点。(代理类是给定类型的类的所有方法) -
target
: 限制匹配是给定类型的实例的目标对象(被代理对象)的连接点。(目标对象是给定类型的类的所有方法) -
args
: 匹配参数是给定类型的连接点。(方法入参是给定类型的方法) -
@target
: 匹配有给定注解的执行对象的class的连接点。(目标对象class上有给定注解的类的所有方法) -
@args
: 匹配实际传递的参数的运行时类型有给定的注解的连接点。(方法入参上有给定注解) -
@within
: 匹配有给定注解的类型的连接点。(class上有给定注解的class的所有方法) -
@annotation
: 匹配连接点的subject有给定注解的连接点。(方法上有给定注解的方法)
表达式使用示例
execution
格式:
// 带?的表示可选
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?)
- 可选)方法修饰符匹配 modifier-pattern
- 方法返回值匹配 ret-type-pattern
- (可选)类路径匹配 declaring-type-pattern
- 方法名和参数匹配 name-pattern(param-pattern)
- (可选)异常类型匹配 throws-pattern
示例:
// 匹配所有方法
execution(* *(..))
// 匹配 com.bytebeats.spring4.aop.xml.service.UserService中所有的公有方法
execution(public * com.bytebeats.spring4.aop.xml.service.UserService.*(..))
// 匹配com.bytebeats.spring4.aop.xml.service 包及其子包下的所有方法
execution(* com.bytebeats.spring4.aop.xml.service..*.*(..))
// 匹配IndexDao所有第一个入参为String的方法(可以有一个或多个入参,但第一个必须是String)
execution(* com.jiyongjun.spring.core.aop.aspectj.pointcut.IndexDao.*(String, ..))
within
within与execution相比,粒度更大,仅能实现到包和接口、类级别。而execution可以精确到方法的返回值,参数个数、修饰符、参数类型等
//匹配com.chenss.dao包中的任意方法
@Pointcut("within(com.chenss.dao.*)")
//匹配com.chenss.dao包及其子包中的任意方法
@Pointcut("within(com.chenss.dao..*)")
args
args表达式的作用是匹配指定参数类型和指定参数数量的方法,与包名和类名无关。
args
同execution
不同的地方在于:
-
args
匹配的是 运行时 传递给方法的参数类型 -
execution(* *(java.io.Serializable))
匹配的是方法在 声明时 指定的方法参数类型。
this
指向代理对象。
- JDK代理时,指向接口和代理类proxy
- cglib代理时,指向接口和子类(不使用proxy)。
target
指向目标对象。
此处需要注意的是,如果配置设置proxyTargetClass=false
,或默认为false,则是用JDK代理,否则使用的是CGLIB代理。JDK代理的实现方式是基于接口实现,代理类继承Proxy
,实现接口。而CGLIB通过继承被代理的类来实现。
所以使用target
会保证目标不变,关联对象不会受到这个设置(proxyTargetClass
)的影响。而使用this
时,会根据该选项的设置,判断是否能找到对象。
//目标对象,也就是被代理的对象。限制目标对象为com.chenss.dao.IndexDaoImpl类实例
@Pointcut("target(com.chenss.dao.IndexDaoImpl)")
//当前对象,也就是代理对象,代理对象是通过代理目标对象的方式获取新的对象,与目标对象并非同一个
@Pointcut("this(com.chenss.dao.IndexDaoImpl)")
//具有@Chenss的目标对象中的任意方法
@Pointcut("@target(com.chenss.anno.Chenss)")
//等同于@target
@Pointcut("@within(com.chenss.anno.Chenss)")
@annotation
// 匹配带有com.chenss.anno.Chenss注解的方法
@Pointcut("@annotation(com.chenss.anno.Chenss)")
bean
Spring特有。
// 匹配名称为dao1的bean上的任意方法
@Pointcut("bean(dao1)")
// 匹配名称以dao开头的bean上的任意方法
@Pointcut("bean(dao*)")