prepareRefresh()

57 阅读1分钟

prepareRefresh();

protected void prepareRefresh() {
		// Switch to active. 给当前spring设置状态,
		this.startupDate = System.currentTimeMillis();
        // 设置spring容器的状态(为后续spring容器关闭和状态判断的时候作为依据)
		this.closed.set(false);
        // 设置spring容器的状态(为后续spring容器关闭和状态判断的时候作为依据)
		this.active.set(true);

		if (logger.isDebugEnabled()) {
			if (logger.isTraceEnabled()) {
				logger.trace("Refreshing " + this);
			} else {
				logger.debug("Refreshing " + getDisplayName());
			}
		}

		// Initialize any placeholder property sources in the context environment.
		// 该方法是一个空方法,是用来对spring容器进行扩展的,如果你有需要可以在里面获取系统参数
		initPropertySources();

		// Validate that all properties marked as required are resolvable:
		// see ConfigurablePropertyResolver#setRequiredProperties
		getEnvironment().validateRequiredProperties();

		// Store pre-refresh ApplicationListeners...
		if (this.earlyApplicationListeners == null) {
			this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
		} else {
			// Reset local application listeners to pre-refresh state.
			this.applicationListeners.clear();
			this.applicationListeners.addAll(this.earlyApplicationListeners);
		}

		// Allow for the collection of early ApplicationEvents,
		// to be published once the multicaster is available...
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}

上述高光的方法,其实是一个空方法,这个方法的作用是给spring容器在启动的前期做一些系统参数的设置,或者说检查用的,比如像springBoot,他就是在这里对springBoot服务启动的时候做一些环境设置。