Spring源码之ApplicationContext(十)过程刷新
2016-03-21 23:52
323 查看
ApplicationContext的初始化的最后一步就是要刷新过程,什么是刷新过程?其实Spring关闭的时候调用stop方法来结束生命周期,通常用来配置后台程序,在启动后一直运行(如对MQ进行轮询等)。我们先来看调用的时序图。(相关资源可到这里下载:http://pan.baidu.com/s/1sjSo9a9)
首先,Spring先调用的是finishRefresh()的方法。
之后,我们会发现,Spring的ApplicationContext启动或停止时,它会通过LifecycleProcessor来与所有声明的bean的周期做状态,而LifecycleProcessor的使用前首先需要初始化。
当做finishRefresh()方法,还有两个的方法调用也是值得我们去跟踪的,那就是onRefresh()的方法,及它间接调用了startBeans的方法。
最后,我们来看一下publishEvent的源码。
首先,Spring先调用的是finishRefresh()的方法。
/** * Finish the refresh of this context, invoking the LifecycleProcessor's * onRefresh() method and publishing the * {@link org.springframework.context.event.ContextRefreshedEvent}. */ protected void finishRefresh() { // Initialize lifecycle processor for this context. initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
之后,我们会发现,Spring的ApplicationContext启动或停止时,它会通过LifecycleProcessor来与所有声明的bean的周期做状态,而LifecycleProcessor的使用前首先需要初始化。
/** * Initialize the LifecycleProcessor. * Uses DefaultLifecycleProcessor if none defined in the context. * @see org.springframework.context.support.DefaultLifecycleProcessor */ protected void initLifecycleProcessor() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) { this.lifecycleProcessor = beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class); if (logger.isDebugEnabled()) { logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]"); } } else { DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor(); defaultProcessor.setBeanFactory(beanFactory); this.lifecycleProcessor = defaultProcessor; beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor); if (logger.isDebugEnabled()) { logger.debug("Unable to locate LifecycleProcessor with name '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "': using default [" + this.lifecycleProcessor + "]"); } } }
当做finishRefresh()方法,还有两个的方法调用也是值得我们去跟踪的,那就是onRefresh()的方法,及它间接调用了startBeans的方法。
@Override public void onRefresh() { startBeans(true); this.running = true; }
private void startBeans(boolean autoStartupOnly) { Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans(); Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>(); for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) { Lifecycle bean = entry.getValue(); if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) { int phase = getPhase(bean); LifecycleGroup group = phases.get(phase); if (group == null) { group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly); phases.put(phase, group); } group.add(entry.getKey(), bean); } } if (phases.size() > 0) { List<Integer> keys = new ArrayList<Integer>(phases.keySet()); Collections.sort(keys); for (Integer key : keys) { phases.get(key).start(); } } }
最后,我们来看一下publishEvent的源码。
/** * Publish the given event to all listeners. * @param event the event to publish (may be an {@link ApplicationEvent} * or a payload object to be turned into a {@link PayloadApplicationEvent}) * @param eventType the resolved event type, if known * @since 4.2 */ protected void publishEvent(Object event, ResolvableType eventType) { Assert.notNull(event, "Event must not be null"); if (logger.isTraceEnabled()) { logger.trace("Publishing event in " + getDisplayName() + ": " + event); } // Decorate event as an ApplicationEvent if necessary ApplicationEvent applicationEvent; if (event instanceof ApplicationEvent) { applicationEvent = (ApplicationEvent) event; } else { applicationEvent = new PayloadApplicationEvent<Object>(this, event); if (eventType == null) { eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType(); } } // Multicast right now if possible - or lazily once the multicaster is initialized if (this.earlyApplicationEvents != null) { this.earlyApplicationEvents.add(applicationEvent); } else { getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType); } // Publish event via parent context as well... if (this.parent != null) { if (this.parent instanceof AbstractApplicationContext) { ((AbstractApplicationContext) this.parent).publishEvent(event, eventType); } else { this.parent.publishEvent(event); } } }
相关文章推荐
- Spring源码之ApplicationContext(九)初始化剩余的单例
- Android 热补丁动态修复框架小结
- android的常用布局LinearLayout和RelativeLayout
- Spring源码之ApplicationContext(八)消息广播
- 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(四)功能补充