1. 程式人生 > >Spring web環境容器啟動流程

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