Spring源码之ApplicationContext(八)消息广播
2016-03-21 23:47
369 查看
当我们定义某个类,并将这个类继承ApplicationEvent,那么这个类就是一个监听事件的类。如果我们定义了个类继承了ApplicatioListener的话,那么这个类就是个监听器了。我们可以在Spring的配置文件中加入:<bean id=”testListener” class=”com.test.event.TestListener”/>当程序运行时,Spring会将发出的事件转给我们定义的监听器进行处理。这个过程是如何实现,我们来看一下时序图。(相关资源可到这里下载:http://pan.baidu.com/s/1sjSo9a9)
![](http://img.blog.csdn.net/20160321234734086?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
我们跟踪代码到initApplicationEventMulticaster的函数中,我们可以看,这个函数无非就是做两件事的判断:
1) 如果用户自定义了广播器,那么使用用户自定义的事件广播器。
2) 如果用户没有自定义事件广播器,那么使用默认的ApplicationEventMulticaster.
对于上面的代码,我们可能会更关心的是SimpleApplicationEventMulticaster的类,我们跟踪代码进入,可能我们对于类中的multicastEvent的函数会更兴趣。
从这段代码,我们可以推断,当产生Spring事件的时候会默认使用SimpleApplicationEventMulticaster的multicastEvent来广播事件,遍历所有监听器,并使用监听中的onApplicationEvent方法来进行监听的处理。那么Spring注册监听器后又做了什么呢?我们来看下面代码。
我们跟踪代码到initApplicationEventMulticaster的函数中,我们可以看,这个函数无非就是做两件事的判断:
1) 如果用户自定义了广播器,那么使用用户自定义的事件广播器。
2) 如果用户没有自定义事件广播器,那么使用默认的ApplicationEventMulticaster.
/** * Initialize the ApplicationEventMulticaster. * Uses SimpleApplicationEventMulticaster if none defined in the context. * @see org.springframework.context.event.SimpleApplicationEventMulticaster */ protected void initApplicationEventMulticaster() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) { this.applicationEventMulticaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); if (logger.isDebugEnabled()) { logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]"); } } else { this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory); beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); if (logger.isDebugEnabled()) { logger.debug("Unable to locate ApplicationEventMulticaster with name '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "': using default [" + this.applicationEventMulticaster + "]"); } } }
对于上面的代码,我们可能会更关心的是SimpleApplicationEventMulticaster的类,我们跟踪代码进入,可能我们对于类中的multicastEvent的函数会更兴趣。
@Override public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) { ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event)); for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) { Executor executor = getTaskExecutor(); if (executor != null) { executor.execute(new Runnable() { @Override public void run() { invokeListener(listener, event); } }); } else { invokeListener(listener, event); } } }
从这段代码,我们可以推断,当产生Spring事件的时候会默认使用SimpleApplicationEventMulticaster的multicastEvent来广播事件,遍历所有监听器,并使用监听中的onApplicationEvent方法来进行监听的处理。那么Spring注册监听器后又做了什么呢?我们来看下面代码。
/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }
相关文章推荐
- The project target (Android N (Preview)) was not properly loaded.
- Spring源码之ApplicationContext(七)获取消息资源
- AndroidAutoLayout 屏幕适配
- Android中SQLite数据库小计
- Android——关于Activity跳转的返回(无返回值和有返回值)——无返回值
- Spring源码之ApplicationContext(六)注册BeanPostProcessor
- Objective-C探究alloc方法的实现
- cocos2dx三种定时器的使用
- Objective-C语法之代码块(block)的使用
- Android中Fragment中使用Intent
- Spring源码之ApplicationContext(五)激活BeanFactory
- Android Smart Image View
- Android学习之友盟开发这些坑
- Android群英传笔记——第五章:Android Scroll分析
- Android群英传笔记——第五章:Android Scroll分析
- Spring源码之ApplicationContext(四)功能补充
- Android课程---Activity的跳转与传值(转自网上)
- android studio单元测试
- android--Activity有返回值的跳转
- Scala 小程序记录(学习期间的代码片段)