spring boot 源碼分析(六) 配置文件加載之StandardServletEnvironment

1、前言java

前面,咱們經過源碼的方法解析瞭如下StandardEnvionment,本章,咱們繼續解析關於webweb

的envionment,叫作StandardServletEnvironment.spring

2、類圖ide

咱們經過idea自帶的類圖生成工具生成關於該類的類圖工具

3、源碼解析idea

package org.springframework.web.context.support;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySource.StubPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.jndi.JndiPropertySource;
import org.springframework.lang.Nullable;
import org.springframework.web.context.ConfigurableWebEnvironment;

//用於web應用,全部基於web的ApplicationContext 類默認都會初始化一個實例
public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment {

	/** Servlet context init parameters property source name: {@value} */
	public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";

	/** Servlet config init parameters property source name: {@value} */
	public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams";

	/** JNDI property source name: {@value} */
	public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";

    //使用超類貢獻和適用於基於servlet標準環境定製屬性源:
    //servletConfigInitParams
    //servletContextInitParams
    //jndiProperties
    //優先級  servletConfigInitParams>servletContextInitParams>
    //jndiProperties>StandardEnvironment
    //(system properties and environment variables)
    //基於servlet的屬性源在這一步會先存起來,當ServletObject變得可用的時候,他會徹底初始化。
	@Override
	protected void customizePropertySources(MutablePropertySources propertySources) {
		propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
		propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
		if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
			propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
		}
		super.customizePropertySources(propertySources);
	}

	@Override
	public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {
		WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
	}

}

咱們經過源碼能夠得出,他的addLast順序,嚴格按照優先級code

servletConfigInitParams>servletContextInitParams>jndiProperties>StandardEnvironment(system properties and environment variables)的順序來的。get

相關文章
相關標籤/搜索