spring事件@EventListener以及@TransactionalEventListener是如何实现的【基于注解编程】

@EventListener解析

1、@EventListener注解注释解释及说明

1、支持基于@Order注解的排序实现
2、要想支持异步@Async使用这个注解【记得在配置类上面开始异步支持哦@EnableAsync】
还有其他支持具体看注解上面的说明

//注解的具体实现@see EventListenerMethodProcessor
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EventListener {
  ...注解的属性
}

疑问


1、在某个方法上面加上了@EventListener注解后,spring如何让他成为一个监听器的?

2、spring加载监听器后,放哪里的?


  • EventListenerMethodProcessor spring通过EventListenerMethodProcessor来实现对注解@EventListener的解析

1)、EventListenerMethodProcessor是什么时候进行初始化的?

  • 先将beanDefinition定义注册到spring-beanFactory中

流程AnnotationConfigApplicationContext
  -->构造函数(初始化)
    --》this.reader = new AnnotatedBeanDefinitionReader(this)  初始化函数中,实现了对AnnotatedBeanDefinitionReader的初始化,并且把spring容器给当做参数设置了进去
      --》AnnotatedBeanDefinitionReader初始化的时候
        --》AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)
          --》AnnotationConfigUtils.registerAnnotationConfigProcessors(registry, null)
            1)、DefaultListableBeanFactory通过参数registry(找bean工厂)
              if (registry instanceof DefaultListableBeanFactory) {
                    return (DefaultListableBeanFactory) registry;
                }
                else if (registry instanceof GenericApplicationContext) {
                    return ((GenericApplicationContext) registry).getDefaultListableBeanFactory();
                }
            2)、给beanFactory注册bean
                1)、org.springframework.context.annotation.internalConfigurationAnnotationProcessor ConfigurationClassPostProcessor 配置后置处理器
                   处理@Configuration标签的
                2)、org.springframework.context.annotation.internalAutowiredAnnotationProcessor AutowiredAnnotationBeanPostProcessor 自动注入处理器
                   处理Autowired的、@Value等
                3)、org.springframework.context.annotation.internalCommonAnnotationProcessor CommonAnnotationBeanPostProcessor JSR-250规范
                4)、org.springframework.context.annotation.internalPersistenceAnnotationProcessor JPA支持
                5)、org.springframework.context.event.internalEventListenerProcessor EventListenerMethodProcessor 事件监听处理器
                   处理@EventListener注解支持
                6)、org.springframework.context.event.internalEventListenerFactory DefaultEventListenerFactory 事件监听工厂
                   处理EventListenerFactory接口支持

  • BeanFactoryPostProcessor由于实现了bean工厂后置处理器

//spring容器启动的时候,执行了beanFactory后置处理器
invokeBeanFactoryPostProcessors(beanFactory);
  --》PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())
    --》String[] postProcessorNames =  beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false)将所有的BeanFactoryPostProcessor给找出来
      1)、看其是否实现了PriorityOrdered【有优先级】接口(排序接口)
      2)、排序接口Ordered
      3)、然后通过beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)创建出来【EventListenerMethodProcessor在这里进行实例化】
      4)、循环调用BeanFactoryPostProcessor接口实例对象的postProcessBeanFactory方法

2)、EventListenerMethodProcessor怎么实现对@EventListener的解析

postProcessBeanFactory--BeanFactory后置处理器

beanFactory.getBeansOfType(EventListenerFactory.class找出bean类型为EventListenerFactory)的

拿到的结果是

 1)、org.springframework.context.event.internalEventListenerFactory DefaultEventListenerFactory 基于@EventListener工厂
 2)、org.springframework.transaction.config.internalTransactionalEventListenerFactorTransactionalEventListenerFactory 基于@TransactionalEventListener工厂

一、通过BeanFactory后置处理器
【找到EventListenerFactory,设置起】
postProcessBeanFactory
  1)、beanFactory.getBeansOfType(EventListenerFactory.class找出bean类型为EventListenerFactory)的
  2)、基于order排个序
  3)、调用getBean方法去(实例化bean--如果没有)
  4)、List<EventListenerFactory> eventListenerFactories然后添加到当前类的属性中
    1)、org.springframework.context.event.internalEventListenerFactory DefaultEventListenerFactory 基于@EventListener工厂
    2)、org.springframework.transaction.config.internalTransactionalEventListenerFactorTransactionalEventListenerFactory 基于@TransactionalEventListener工厂

2、spring加载监听器后,放哪里的?

二、SmartInitializingSingleton 接口执行时机【在bean已经完成实例化之后---bean已经创建好了,不需要做其他事情了】例如:初始化、属性设置、invoker、代理等等
  1)、String[] beanNames = beanFactory.getBeanNamesForType(Object.class) 通过bean工厂把所有为Object类型的bean给找出来
  2)、processBean(final String beanName, final Class<?> targetType)然后循环调用
    1)、看这个类里面是不是有AnnotationUtils.isCandidateClass(targetType, EventListener.class)注解
    2)、把这个类里面加了@EventListener注解的方法找出来
    3)、然后加入把相关的方法接入到context.addApplicationListener(applicationListener)中
       找看看当前方法支持哪种EventListenerFactory类型factory.supportsMethod(method),找到了处理好了就break

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值