【问题标题】:Difference between ContextLoaderListener and ContextLoaderServletContextLoaderListener 和 ContextLoaderServlet 的区别
【发布时间】:2011-02-07 06:57:53
【问题描述】:

我知道getWebApplicationContext()通过ContextLoaderListener或者ContextLoaderServlet找到根WebApplicationContext

但我需要知道区别以及何时使用它?

【问题讨论】:

    标签: java spring servlets


    【解决方案1】:

    ContextLoaderServlet 的 javadoc 说明了一切:

    请注意,对于实现 Servlet API 2.4 或更高版本的容器,该类已被弃用,取而代之的是 ContextLoaderListener。

    显然,在 Servlet API 2.4 之前,监听器与 servlet 的初始化顺序并不受规范的强制。因此,要确保 Spring 上下文在 Servlet 2.3 及更低版本容器中的任何其他 servlet 之前正确加载,您需要使用 ContextLoaderServlet 并将其作为启动时加载的第一个。查看该链接了解更多详情。

    【讨论】:

      【解决方案2】:

      上下文加载器加载上下文配置文件 ex(在 web.xml 中):

      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
      </context-param>
      
      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      

      【讨论】:

      • 问题是关于ContextLoadListenerContextLoadServlet,两者都调用ContextLoader——负责加载上下文配置文件的类。 laz 的回答更直接针对 op 的原始问题。
      猜你喜欢
      • 2023-03-03
      • 2016-09-12
      • 2011-11-20
      • 2012-06-14
      • 2016-06-09
      • 2014-12-15
      • 2012-02-19
      • 2013-08-25
      相关资源
      最近更新 更多