Spring web环境容器启动流程
来源:互联网 发布:linux虚拟主机销售系统 编辑:程序博客网 时间:2024/05/19 05:31
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. */@Overridepublic 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
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);}
@Overrideprotected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment();}
@Overridepublic 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方法
@Overrideprotected 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方法
@Overrideprotected void onRefresh() { this.themeSource = UiApplicationContextUtils.initThemeSource(this);}
阅读全文
0 0
- Spring web环境容器启动流程
- web容器启动spring容器
- web容器启动Spring
- Spring IoC容器在Web环境中的启动源码阅读
- Spring ioc容器启动流程
- web项目启动Spring容器
- Web容器 与Spring容器 启动
- Spring Boot Web启动流程
- springMVC中spring容器启动流程
- Spring ioc容器启动流程—obtainFreshBeanFactory
- spring(2) spring ioc容器在Web容器中的启动
- Spring boot源码分析-AnnotationConfigApplicationContext非web环境下的启动容器(2)
- Spring boot源码分析-AnnotationConfigEmbeddedWebApplicationContext默认web环境下的启动容器(3)
- Web容器启动时加载Spring
- Spring 学习-------WebApplication (web 容器)启动
- Web容器启动时加载Spring分析
- web是如何启动spring容器的?
- web是如何启动spring容器的
- 《python核心编程第二版第七章习题补充》
- spring整合dwr架构(ftl+ssm)以及dwr.xml配置,原理,优缺点讲解,面试题总结
- 理解Linux系统/etc/init.d目录和/etc/rc.local脚本
- redis的基本存储结构的简单使用
- 怎样用通俗的语言解释REST,以及RESTful?
- Spring web环境容器启动流程
- jQuery 事件——关于Select选中
- 树状数组模板
- 套接字缓冲区 boost asio
- 基于maven使用IDEA创建多模块项目
- 记录一个过滤网址
- HDU 4810 Wall Painting
- 工具快捷键、编程风格、常用命令、实用工具等
- 我知道的新技术(持续更新)