`

寻找spring框架的启动入口 ContextLoaderListener

阅读更多

springweb下的入口在配置文件web.xml的监听器中

<listener>

        <listener-class>

           org.springframework.web.context.ContextLoaderListener

        </listener-class>

</listener>

<context-param>

     <param-name>contextConfigLocation</param-name>

      <param-value>classpath:conf/spring/applicationContext.xml</param-value>

</context-param>

上述是在web.xml中的配置信息。

 

 

//实现了接口ServletContextListener,也就是说他必须实现contextDestroyed, contextInitialized这两个方法

public class ContextLoaderListener implements ServletContextListener {

       private ContextLoader contextLoader;

       /**

       * Initialize the root web application context.

       */

//Spring框架由此启动, contextInitialized也就是监听器类的main入口函数

       public void contextInitialized(ServletContextEvent event) {

              this.contextLoader = createContextLoader();

              this.contextLoader.initWebApplicationContext(event.getServletContext());

       }

       /**

       * Create the ContextLoader to use. Can be overridden in subclasses.

       * @return the new ContextLoader

       */                                            

       protected ContextLoader createContextLoader() {

              return new ContextLoader();

       }

       /**

       * Return the ContextLoader used by this listener.

       * @return the current ContextLoader

       */

       public ContextLoader getContextLoader() {

              return this.contextLoader;

       }

       /**

       * Close the root web application context.

       */

       public void contextDestroyed(ServletContextEvent event) {

              if (this.contextLoader != null) {

                     this.contextLoader.closeWebApplicationContext(event.getServletContext());

              }

       }

}

 

总的来说这个入口非常简单,所有实现都隐藏在ContextLoader类里,我们在下一篇的内容中讨论ContextLoader,如果你不知道为什么这里是程序的入口,那么复习一下ServletContextListener 接口和监听器的相关知识吧

分享到:
评论

相关推荐

    Spring的监听器ContextLoaderListener的作用

    ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener 接口,在 web.xml 文件中配置这个监听器,启动...

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    一、Web项目中使用Spring ...在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 Service 对象

    java解决org.springframework.web.context.ContextLoaderListener

    java解决org.springframework.web.context.ContextLoaderListener

    Spring MVC 框架应用实例

    org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;encodingFilter org.springframework.web.filter.CharacterEncodingFilter &lt;param-name&gt;encoding ...

    Spring与Web环境集成.pdf

    Spring框架提供了一个监听器ContextLoaderListener,就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中。同时,Spring还提供了一个客户端工具...

    SpringMVC框架搭建及详解.pdf

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener ``` * 配置context-param: ```xml &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath:config/applicationContext.xml ``` ...

    web.xml中ContextLoaderListener的运行过程解析

    web.xml中ContextLoaderListener的运行

    spring MVC配置详解

    Spring MVC 是一个基于 DispatcherServlet 的 MVC 框架,它是当前主流的 Web 框架之一。要想灵活运用 Spring MVC 来应对大多数的 Web 开发,就必须要掌握它的配置及原理。 一、Spring MVC 环境搭建 1. jar 包引入 ...

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....

    Spring中ApplicationContext加载机制

    ApplicationContext 是 Spring 框架中的核心组件之一,负责加载和管理应用程序中的 Bean 对象。在 Web 应用程序中,ApplicationContext 的加载机制是非常重要的, Spring 提供了多种方式来加载 ApplicationContext。...

    整合struts2和spring源代码(可以直接在tomcat中运行)

    org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认,一定要放在WEB-INF中并文件名为...

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的入口。下面将详细介绍五种获取 WebApplicationContext 和 ...

    springweb3.0MVC注解(附实例)

    org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--&gt; &lt;servlet-name&gt;annomvc ...

    DispatcherServlet 和 ContextLoaderListener 区别

    NULL 博文链接:https://angie.iteye.com/blog/2334955

    Spring MVC 入门实例

    这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...

    Spring-5.1.5源码

    Spring framework 5.1.5源码 Main: spring-web/org.springframework.web.context.ContextLoaderListener

    ssh框架搭建教程.doc

    SSH 框架是一种常用的 Java Web 应用程序框架,它集成了 Struts、Spring 和 Hibernate 等多种技术,提供了一个强大且灵活的开发平台。下面是 SSH 框架搭建教程的知识点总结: 一、创建 WEB 工程,添加 Struts 支持 ...

    ssh(structs,spring,hibernate)框架中的上传下载

    SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文选用的数据库为Oracle 9i,当然你可以在不改动代码的情况下,通过配置文件的调整将其移植到任何具有Blob字段类型的数据库...

    spring源代码解析

    Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...

    spring和hibernate整合

    org.springframework.web.context.ContextLoaderListener &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt;

Global site tag (gtag.js) - Google Analytics