Spring web环境容器启动流程

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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值