欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring学习之AOP

程序员文章站 2022-05-24 23:44:39
...

AOP原理

@EnableAspectJAutoProxy:核心是这个,AOP要起作用,就是靠他启动

//导入了此类,点进去看
@Import(AspectJAutoProxyRegistrar.class)
public @interface  EnableAspectJAutoProxy {

   //proxyTargetClass属性,默认false,采用JDK动态代理织入增强(实现接口的方式);如果设为true,则采用CGLIB动态代理织入增强

   boolean proxyTargetClass() **default** **false**;

  //通过aop框架暴露该代理对象,aopContext能够访问

   boolean exposeProxy() **default** **false**;

}

AspectJAutoProxyRegistrar它引入AspectJAutoProxyRegistrar, 并实现了ImportBeanDefinitionRegistrar接口

ImportBeanDefinitionRegistrar接口作用: 能给容器中自定义注册组件

AspectJAutoProxyRegistrar里可以自定义注册一些bean

@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;

}
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) {

      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);
         }
      }
   }

}

分析创建和注册AnnotationAwareAspectJAutoProxyCreator的流程:

1)、register()传入配置类,准备创建ioc容器

2)、注册配置类,调用refresh()刷新创建容器;

3)、registerBeanPostProcessors(beanFactory);注册bean的后置处理器来方便拦截bean的创建

​ 1)、 先获取ioc容器已经定义了的需要创建对象的所有BeanPostProcessor

​ 2)、给容器中加别的BeanPostProcessor

​ 3)、优先注册实现了PriorityOrdered接口的BeanPostProcessor;

​ 4)、再给容器中注册实现了Ordered接口的BeanPostProcessor;

​ 5)、注册没实现优先级接口的BeanPostProcessor;

​ 6)、注册BeanPostProcessor,实际上就是创建BeanPostProcessor对象,保存在容器中;

创建internalAutoProxyCreator的BeanPostProcessor【其实就是AnnotationAwareAspectJAutoProxyCreator】

1)、创建Bean的实例

​ 2)、populateBean;给bean的各种属性赋值

​ 3)、initializeBean:初始化bean;

​ 1)、invokeAwareMethods():处理Aware接口的方法回调

​ 2)、applyBeanPostProcessorsBeforeInitialization():应用后置处理器的postProcessBeforeInitialization()

​ 3)、invokeInitMethods();执行自定义的初始化方法

​ 4)、applyBeanPostProcessorsAfterInitialization();执行后置处理器的postProcessAfterInitialization();

​ 4)、BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)创建成功;–》aspectJAdvisorsBuilder

​ 7)、把BeanPostProcessor注册到BeanFactory中;

​ beanFactory.addBeanPostProcessor(postProcessor);

注意:以上是创建和注册AnnotationAwareAspectJAutoProxyCreator的过程

​ AnnotationAwareAspectJAutoProxyCreator => InstantiationAwareBeanPostProcessor