背景
其实现在springboot是默认开启AOP的。
@EnableAspectJAutoProxy这个注解就是手动开启AOP的一个注解。
想看看Spring在背后做了哪些事情就得从源码入手一起来看下。
EnableAspectJAutoProxy
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
/**
* Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
* to standard Java interface-based proxies. The default is {@code false}.
*/
boolean proxyTargetClass() default false;
/**
* Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal}
* for retrieval via the {@link org.springframework.aop.framework.AopContext} class.
* Off by default, i.e. no guarantees that {@code AopContext} access will work.
* @since 4.3.1
*/
boolean exposeProxy() default false;
}
@EnableAspectJAutoProxy注解中两个参数 proxyTargetClass 、exposeProxy
- proxyTargetClass
制AOP代理的具体实现方式。英文注释很简单哈,是否开启CGLIB(基于子类)的代理,为true 的话使用CGLIB,为false的话使用基于标准 Java 接口的代理。默认使用JDK的动态代理。 - exposeProxy
控制代理的暴露方式。是否暴露当前代理对象为ThreadLocal模式。可以解决内部调用不能使用切面的问题,为true的话可以获取代理对象,通过代理对象调用方法进入切面。
//实例伪代码
T proxy=(T) AopContext.currentProxy();
这里核心是@Import(AspectJAutoProxyRegistrar.class)
AspectJAutoProxyRegistrar
class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
/**
* Register, escalate, and configure the AspectJ auto proxy creator based on the value
* of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing
* {@code @Configuration} class.
*/
@Override
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//装载bean
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
AnnotationAttributes enableAspectJAutoProxy =
AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy != null) {
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
}
}
}
通过registerBeanDefinitions方法向Spring容器中注册bean实例。
重点是 AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry); 这句。
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry) {
return registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry, (Object)null);
}
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, @Nullable Object source) {
return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
}
AopConfigUtils这个工具类的主要作用就是把AnnotationAwareAspectJAutoProxyCreator这个类定义为BeanDefinition放到spring容器中,具体怎么实现装载的可以参看ImportBeanDefinitionRegistrar 这个接口。Spring官方在动态注册bean时,大部分套路其实是使用ImportBeanDefinitionRegistrar接口。
所有实现了该接口的类都会被ConfigurationClassPostProcessor处理,ConfigurationClassPostProcessor实现了BeanFactoryPostProcessor接口,所以ImportBeanDefinitionRegistrar中动态注册的bean是优先于依赖其的bean初始化的,也能被aop、validator等机制处理。
这里重点类就是被注册到容器中的AnnotationAwareAspectJAutoProxyCreator这个类。也是Spring AOP 最为核心的一个类。
AnnotationAwareAspectJAutoProxyCreator
从类图上可以大致了解AnnotationAwareAspectJAutoProxyCreator这个创建器是用来做什么的。
- 实现了一系列Aware的接口
Aware,翻译过来是知道的,已感知的,意识到的,所以这些接口从字面意思就是能感知到所有Aware前面的含义。
在Bean装载的时候获取BeanFactory(Bean容器),Bean的ClassLoader - 实现了Ordered接口
对切点上的切面进行排序 - 继承了PorxyConfig
ProxyConfig中主要封装了代理的通用处理逻辑,比如设置目标类,设置使用cglib还是java proxy等一些基础配置. - 实现了BeanPostProcessor接口
BeanPostProcessor是Spring IOC容器给我们提供的一个扩展接口。也是AOP实现代理的核心。
public interface BeanPostProcessor</