一、前言
前面,我们通过源码的方法解析了以下StandardEnvionment,本章,我们继续解析关于web
的envionment,叫做StandardServletEnvironment.
二、类图
我们通过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顺序,严格按照优先级
servletConfigInitParams>servletContextInitParams>jndiProperties>StandardEnvironment(system properties and environment variables)的顺序来的。