文章目录

  • finishRefresh
  • initLifecycleProcessor
  • getLifecycleProcessor().onRefresh();
  • publishEvent(new ContextRefreshedEvent(this));
  • LiveBeansView.registerApplicationContext(this);
  • 完结撒花


finishRefresh

前面大概讲了一下spring通过反射创建bean,对bean进行属性赋值,这时候spring容器大体都加载完成了,单例bean都初始化完成,属性也都赋值了,进行最后一步了,完成刷新方法了
源码:

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.
    // 将spring容器注册到LiveBeansView
		LiveBeansView.registerApplicationContext(this);
	}

initLifecycleProcessor

protected void initLifecycleProcessor() {
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    // 判断BeanFactory是否已经存在生命周期处理器(固定使用beanName=lifecycleProcessor)
    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
			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 + "]");
			}
		}
	}

getLifecycleProcessor().onRefresh();

// Internal helpers
	// 根据阶段执行Lifecycle的start方法
	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.isEmpty()) {
			List<Integer> keys = new ArrayList<Integer>(phases.keySet());
			Collections.sort(keys);
			for (Integer key : keys) {
				phases.get(key).start();
			}
		}
	}

publishEvent(new ContextRefreshedEvent(this));

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 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...
    // 通过parent发布时间
		if (this.parent != null) {
			if (this.parent instanceof AbstractApplicationContext) {
				((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
			}
			else {
				this.parent.publishEvent(event);
			}
		}
	}

LiveBeansView.registerApplicationContext(this);

// 向MBeanServer 托管Live Beans,也不知道要干啥…

完结撒花

至此,spring源码的主流程基本都过了一遍,后面对一些单独的功能进行单独的讲.回顾spring的流程,就是创建bean的流程,然后就是各种后置处理器,bean工厂的后置处理器,和bean后置处理器,这些处理器做了很多的时候,这些处理器的执行时机也十分重要,基本就是在容器的创建过程中和bean创建前后~,spring对处理器进行了一些扩展,理解这些后置处理器十分重要