Spring源码之ApplicationContext(十)过程刷新
来源:互联网 发布:单片机怎样产生1赫兹 编辑:程序博客网 时间:2024/06/06 19:08
ApplicationContext的初始化的最后一步就是要刷新过程,什么是刷新过程?其实Spring关闭的时候调用stop方法来结束生命周期,通常用来配置后台程序,在启动后一直运行(如对MQ进行轮询等)。我们先来看调用的时序图。(相关资源可到这里下载:http://pan.baidu.com/s/1sjSo9a9)
首先,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的方法。
@Overridepublic 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 necessaryApplicationEvent 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 initializedif (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);}}}
0 0
- Spring源码之ApplicationContext(十)过程刷新
- Spring源码之ApplicationContext(一)
- spring源码阅读(五)之ApplicationContext
- Spring源码之ApplicationContext(二)准备工作
- Spring源码之ApplicationContext(三)加载BeanFactory
- Spring源码之ApplicationContext(四)功能补充
- Spring源码之ApplicationContext(五)激活BeanFactory
- Spring源码之ApplicationContext(六)注册BeanPostProcessor
- Spring源码之ApplicationContext(八)消息广播
- spring applicationContext 加载过程
- Spring 源码分析:ApplicationContext
- Spring 源码分析:ApplicationContext- -
- Spring源码解析-applicationContext
- Spring源码解析-applicationContext
- spring源码-ApplicationContext
- Spring Framework源码(二):spring beans之ApplicationContext
- spring源码之旅(1)_BeanFactory与ApplicationContext继承体系
- Spring源码之ApplicationContext(七)获取消息资源
- c语言:多项式相加的实现
- 工具类:快速加载 xib(UIView 分类)
- IOS学习 动画和绘画:UIView基础动画
- 【C++】编程小结②-- 字符串&定义变量
- MySql绿色版安装出现的问题总结
- Spring源码之ApplicationContext(十)过程刷新
- ksh
- 又是一年春天里
- PB 数据窗口语法解释
- 小马哥-----高仿机修复摄像头实例说明 其他机型可借鉴
- Redis对于Key的常用操作,基于版本3.0.7
- Android之状态栏通知Notification、NotificationManager详解
- Oracle新建数据库和用户及表空间
- 40个Java多线程问题总结