Spring5 源码阅读笔记(1.5)finishRefresh() 完成刷新

protected void finishRefresh() {
	//清空上下文级别的资源缓存
	clearResourceCaches();

	//初始化上下文的LifecycleProcessor 见1.5.1
	initLifecycleProcessor();

	//调用所有SmartLifecycle的start方法 见1.5.2
	getLifecycleProcessor().onRefresh();

	//发布ContextRefreshedEvent
	publishEvent(new ContextRefreshedEvent(this));

	//注册MBean,通过JMX进行监控和管理
	LiveBeansView.registerApplicationContext(this);
}

1.5.1 initLifecycleProcessor

protected void initLifecycleProcessor() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		//如果有lifecycleProcessor这么一个Bean(实例也好,BeanDefinition也好)
	if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
		//填充上下文的成员变量
		this.lifecycleProcessor =
				beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
		if (logger.isTraceEnabled()) {
			logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
		}
	}
	else {
		//没有lifecycleProcessor这个Bean,新建一个默认的
		DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
		defaultProcessor.setBeanFactory(beanFactory);
		//填充上下文的成员变量
		this.lifecycleProcessor = defaultProcessor;
		//注册Bean到工厂
		beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
		if (logger.isTraceEnabled()) {
			logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
					"[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
		}
	}
}

1.5.2 onRefresh

类 DefaultLifecycleProcessor

@Override
public void onRefresh() {
	startBeans(true);
	this.running = true;
}

跟 startBeans:

private void startBeans(boolean autoStartupOnly) {
	//得到所有的Lifecycle
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<>();
	//收集SmartLifecycle
	lifecycleBeans.forEach((beanName, bean) -> {
		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(beanName, bean);
		}
	});
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<>(phases.keySet());
		Collections.sort(keys);
		for (Integer key : keys) {
			phases.get(key).start();
		}
	}
}

Tips:SmartLifecycle 有 EurekaAutoServiceRegistration

跟 start:

public void start() {
	if (this.members.isEmpty()) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Starting beans in phase " + this.phase);
	}
	Collections.sort(this.members);
	for (LifecycleGroupMember member : this.members) {
		doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
	}
}

跟 doStart:

private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && bean != this) {
		String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isTraceEnabled()) {
				logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
			}
			try {
				//所有的SmartLifecycle的start方法在这里调用
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}

你可能感兴趣的:(#,refresh())