spring源码学习系列2.4-finishRefresh会做什么

阅读更多
spring容器初始化完成后,调用finishRresh

该方法入口wac.refresh()

AbstractApplicationContext#finishRresh
/**
	 * 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();

//发布容器初始化完成事件ContextRefreshedEvent
		// Publish the final event.
		publishEvent(new ContextRefreshedEvent(this));

		// Participate in LiveBeansView MBean, if active.
		LiveBeansView.registerApplicationContext(this);
	}



AbstractApplicationContext#publishEvent
/**
	 * Publish the given event to all listeners.
	 * 

Note: Listeners get initialized after the MessageSource, to be able * to access it within listener implementations. Thus, MessageSource * implementations cannot publish events. * @param event the event to publish (may be application-specific or a * standard framework event) */ public void publishEvent(ApplicationEvent event) { Assert.notNull(event, "Event must not be null"); if (logger.isTraceEnabled()) { logger.trace("Publishing event in " + getDisplayName() + ": " + event); } getApplicationEventMulticaster().multicastEvent(event); if (this.parent != null) { this.parent.publishEvent(event); } }




SimpleApplicationEventMulticaster#multicastEvent
@SuppressWarnings("unchecked")
	public void multicastEvent(final ApplicationEvent event) {
//SourceFilteringListener
//ContextRefreshListener
		for (final ApplicationListener listener : getApplicationListeners(event)) {
			Executor executor = getTaskExecutor();
			if (executor != null) {
				executor.execute(new Runnable() {
					public void run() {
						listener.onApplicationEvent(event);
					}
				});
			}
			else {
				listener.onApplicationEvent(event);
			}
		}
	}


如:springmvc中注册的SourceFilteringListener implements SmartApplicationListener,会在容器初始化后,回调FrameworkServlet#onRefresh以完成其初始化操作

你可能感兴趣的:(spring源码学习系列2.4-finishRefresh会做什么)