您的位置:首页 > 编程语言 > Java开发

Spring web环境容器启动流程

2017-10-30 09:28 796 查看

1.初始化入口

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

public class ContextLoaderListener extends ContextLoader implements ServletContextListener

通过实现ServletContextListener 在ServletContext初始化完成的时候,容器开始初始化。

/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}

2.创建WebApplicationContext实例

if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}

1.从contextClass参数中获取自定义的类

String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);


2.如果没有自定义则获取默认的类

contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());

我们看defaultStrategies初始化

private static final Properties defaultStrategies;

static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}

/**
* Name of the class path resource (relative to the ContextLoader class)
* that defines ContextLoader's default strategy names.
*/
private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";

我们看ContextLoader.properties文件中的信息

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext


总结:默认创建的是XmlWebApplicationContext实例

3.配置和刷新上下文

configureAndRefreshWebApplicationContext(cwac, servletContext);

1.从ServletContext中获取配置文件路径

String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
wac.setConfigLocation(configLocationParam);
}

public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

2.创建环境对象

ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
}

@Override
protected ConfigurableEnvironment createEnvironment() {
return new StandardServletEnvironment();
}


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

3.定制上下文初始化

customizeContext(sc, wac);

获取ApplicationContextInitializer的实现类


List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses =
determineContextInitializerClasses(sc);

protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>
determineContextInitializerClasses(ServletContext servletContext) {

List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> classes =
new ArrayList<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>();

String globalClassNames = servletContext.getInitParameter(GLOBAL_INITIALIZER_CLASSES_PARAM);
if (globalClassNames != null) {
for (String className : StringUtils.tokenizeToStringArray(globalClassNames, INIT_PARAM_DELIMITERS)) {
classes.add(loadInitializerClass(className));
}
}

String localClassNames = servletContext.getInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM);
if (localClassNames != null) {
for (String className : StringUtils.tokenizeToStringArray(localClassNames, INIT_PARAM_DELIMITERS)) {
classes.add(loadInitializerClass(className));
}
}

return classes;
}

public static final String GLOBAL_INITIALIZER_CLASSES_PARAM = "globalInitializerClasses";

public static final String CONTEXT_INITIALIZER_CLASSES_PARAM = "contextInitializerClasses";

进行初始化

for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
initializer.initialize(wac);
}


4.刷新容器

wac.refresh();

刷新的流程是固定的(模版模式)。只是刷新流程中有些方法子类是可以覆盖的。
1.重写了postProcessBeanFactory方法

@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
beanFactory.ignoreDependencyInterface(ServletConfigAware.class);

WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig);
}

2.重写了onRefresh方法

@Override
protected void onRefresh() {
this.themeSource = UiApplicationContextUtils.initThemeSource(this);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: