09. Spring Framework Event
2022-03-16 20:17:11 0 举报
AI智能生成
Spring 事件机制
作者其他创作
大纲/内容
EventListenerMethodProcessor<br>处理 @EventListener 注解
postProcessBeanFactory<br>因为实现了【BeanFactoryPostProcessor】接口,所以会回调这个方法
从容器中获取所有的【EventListenerFactory】并排序<br>保存到【eventListenerFactories】列表中
afterSingletonsInstantiated()<br>因为实现了【SmartInitializingSingleton】接口,所以会在初始完所有 bean 之后回调这个方法
processBean(beanName, type)<br>循环对容器中的每个组件调用次方法处理
找到这个组件所有被 @EventListener 注解标注的方法
factory.createApplicationListener()<br>循环每一个方法,利用工厂创建适配器监听器
把适配器添加注册到容器和事件派发器<br>事件派发器实现了监听器接口<br>最终事件在派发时,首先发给适配器,由适配器找到对应的目标方法通过发射执行<br>
ApplicationEventPublisher<br>事件发布
如果事件属于 ApplicationEvent,就强转<br>不属于就包装成 PayloadApplicationEvent
getApplicationEventMulticaster()<br>从容器中获取事件派发器<br>
multicastEvent()<br>筛选对此事件类型感兴趣的监听器,再对这些监听器调用 onApplicationEvent() 方法
ApplicationListenerMethodAdapter<br>事件监听
onApplicationEvent()<br>因为实现了【ApplicationListener】接口,所以会接收到事件通知
processEvent(event)<br>处理事件
doInvoke(args)<br>利用反射调用事件
0 条评论
下一页