Spring启动篇
来源:互联网 发布:中国气象数据网账号 编辑:程序博客网 时间:2024/06/15 19:51
Spring启动
Spring 中 BeanFactory一般以编程的方式启动,当然ApplicationContext也支持,但其更多的友好支持声明式创建。
容器启动过程可简单理解为:1预加载配置信息,2初始化相关类;一般他们通过Servlet或监听器来完成启动触发。
在Spring中,主要有ContextLoaderListener,ContextLoaderServlet来初始化Spring容器。
ContextLoaderListener继承自ContextLoader同时实现ServletContextListener,可以看下其说明:
Bootstrap listener to start up and shutdown Spring's root {@link WebApplicationContext}.
Simply delegates to {@link ContextLoader}as well as to {@link ContextCleanupListener}.
This listener should be registered after{@link org.springframework.web.util.Log4jConfigListener} in web.xml, if thelatter is used.
当通过ContextLoaderListener启动时,需要在web.xml添加如下配置。
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> <listener>
其与web服务器的生命周期紧密相关,web服务器启动时,服务器声明周期事件触发ContextLoaderListener加载配置文件,启动初始化。其中重要的方法:contextInitialized(),contextDestoryed(),一个用于启动,一个用于销毁。
可以查看下ContextLoader的initWebApplicationContext源码
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {throw new IllegalStateException("Cannot initialize context because there is already a root application context present - " +"check whether you have multiple ContextLoader* definitions in your web.xml!");}Log logger = LogFactory.getLog(ContextLoader.class);servletContext.log("Initializing Spring root WebApplicationContext");if (logger.isInfoEnabled()) {logger.info("Root WebApplicationContext: initialization started");}long startTime = System.currentTimeMillis();try {// Store context in local instance variable, to guarantee that// it is available on ServletContext shutdown.if (this.context == null) {this.context = createWebApplicationContext(servletContext);}if (this.context instanceof ConfigurableWebApplicationContext) {configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);}servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);ClassLoader ccl = Thread.currentThread().getContextClassLoader();if (ccl == ContextLoader.class.getClassLoader()) {currentContext = this.context;}else if (ccl != null) {currentContextPerThread.put(ccl, this.context);}if (logger.isDebugEnabled()) {logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");}if (logger.isInfoEnabled()) {long elapsedTime = System.currentTimeMillis() - startTime;logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");}return this.context;}catch (RuntimeException ex) {logger.error("Context initialization failed", ex);servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);throw ex;}catch (Error err) {logger.error("Context initialization failed", err);servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);throw err;}}
- Spring启动篇
- Spring(一)启动Spring
- spring 问题 启动异常
- Spring容器启动过程
- spring有三种启动方式
- Spring启动后执行
- Spring容器启动过程
- spring有三种启动方式
- Spring启动后执行
- spring +hibernate 启动优化
- Spring启动后执行
- spring启动安全了解
- Spring MVC启动过程
- Spring MVC启动过程
- Spring启动后执行
- spring启动报错
- spring启动流程
- Spring容器启动过程
- windows批处理
- 在mysql数据库中中文排序
- 细节决定了我的实验结果
- Pthon3.x和Python2.x的区别
- 反射之内部类
- Spring启动篇
- Java数据导入(读)Excel文件 解析
- 音视频即时通讯开发平台的开发现状
- POJ 3207 Ikki's Story IV - Panda's Trick(2-SAT判定)
- 使用ES文件浏览器通过Android设备访问Win7电脑上的共享文件
- 内存泄漏检测工具
- Java 输入输出流教程(三)——【管道】(原作:Java IO Tutorial by Jakob Jenkov)
- Android控件复习:按钮(Button)
- OpenCV与Compressive Tracking实现人脸的实时检测与跟踪