Spring source code learning series 2.4 - what will finishRefresh do

After the spring container is initialized, call finishRresh.

The method entry 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();

//Publish container initialization complete event 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.
	 * <p>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);
			}
		}
	}


For example: SourceFilteringListener implements SmartApplicationListener registered in springmvc will call FrameworkServlet#onRefresh to complete its initialization after the container is initialized

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326468415&siteId=291194637