Spring ApplicationContext refresh方法(二)--prepareRefresh

6 篇文章 0 订阅
3 篇文章 0 订阅

Spring ApplicationContext refresh方法(一)

prepareRefresh

prepareRefresh方法同样是抽象类AbstractApplicationContext方法实现的。
见字知意,此方法就是为spring的启动做准备。
首先设置AtomicBoolean原子标志位closed/active。

initPropertySources

	protected void prepareRefresh() {
		this.startupDate = System.currentTimeMillis();
		this.closed.set(false);
		this.active.set(true);

		if (logger.isInfoEnabled()) {
			logger.info("Refreshing " + this);
		}

		// Initialize any placeholder property sources in the context environment
		//
		//
		initPropertySources();

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

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

抽象类中的initPropertySources并未实现任何代码,而是为子类提供了扩展点。
通过使用ide可以发现org.springframework.web.context.support.StaticWebApplicationContext对此进行了实现。
通过名字可以知道是读取环境中的配置用于初始化Servlet相关的属性。

@Override
	protected void initPropertySources() {
		WebApplicationContextUtils.initServletPropertySources(getEnvironment().getPropertySources(),
				this.servletContext, this.servletConfig);
	}

public static void initServletPropertySources(MutablePropertySources sources,
			@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {

		Assert.notNull(sources, "'propertySources' must not be null");
		String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
		if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
			sources.replace(name, new ServletContextPropertySource(name, servletContext));
		}
		name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
		if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
			sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
		}
	}

getEnvironment().validateRequiredProperties()

getEnvironment方法是获取环境,null则会返回new StandardEnvironment()
StandardEnvironment继承AbstractEnvironment,在父类空参构造时会调用
customizePropertySources(this.propertySources);,此方法同样父类未实现由子类重写。

//systemProperties ,systemEnvironment
@Override
   protected void customizePropertySources(MutablePropertySources propertySources) {
   	propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
   	propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
   }

StandardEnvironmentMutablePropertySources包含一个PropertySource的数组。数组类使用的是CopyOnWriteArrayList,其内部使用ReentrantLock保证并发下的顺序性。具体可以查看具体资料。

private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<>();

  回到customizePropertySources方法,方法中向propertySourceList中增加了MapPropertySource,SystemEnvironmentPropertySource两个的实现类。
properties是jvm参数,env是操作系统环境变量。

validateRequiredProperties

validateRequiredProperties校验当前环境中是否缺少必要的参数,必要的参数是通过org.springframework.core.env.AbstractEnvironment#setRequiredProperties设置进去的,这也是一个扩展点,spring refresh中并没有调用此方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值