Spring学习之AOP

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值