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); }
相关文章推荐
- Spring IoC容器在Web环境中的启动源码阅读
- Spring boot源码分析-AnnotationConfigApplicationContext非web环境下的启动容器(2)
- Spring Ioc(1)——web中spring容器的启动初步
- Spring在Web容器启动时执行初始化方法
- Spring基础系列-容器启动流程(2)
- web是如何启动spring容器的?
- spring 在web容器启动时执行初始化方法
- 【Java.Spring.MVC】Spring在Web容器中的启动过程
- 在web项目中如何启动spring容器?
- Spring深入挖掘:Spring在Web容器中是如何启动的
- springMVC中spring容器启动流程
- Shiro在Web环境下集成Spring的大致工作流程
- Spring Boot Web启动流程
- 【spring】IOC容器在WEB容器中的启动
- Spring源代码解析(二):IoC容器在Web容器中的启动
- Spring 学习-------WebApplication (web 容器)启动
- 转:Spring源码分析:IOC容器在web容器中的启动
- ServletContext与Web应用以及Spring容器启动
- 流程图解Spring Framework(二) spring 如何启动容器的---XML配置
- Spring boot 设置启动是否WEB环境