一个标准的Spring Web应用程序(由Roo或“Spring MVC Project”模板创建)将创建一个带有
背景:
我已经按照这种标准方式做了几年。
ContextLoaderListener
和DispatcherServlet
的web.xml。 为什么它们不仅使用DispatcherServlet
并使其加载完整的配置呢?
我理解ContextLoaderListener
应该用于加载与Web无关的内容,而DispatcherServlet
用于加载与Web相关的内容(控制器等)。 这会导致两个上下文:父上下文和子上下文。背景:
我已经按照这种标准方式做了几年。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles Spring requests -->
<servlet>
<servlet-name>roo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这经常会导致两个上下文和它们之间的依赖关系出现问题。过去,我总是能找到解决方案,而且我强烈感觉这使软件结构/架构始终更好。但现在我面临着两个上下文事件问题。
-- 然而,这让我重新考虑了这两个上下文模式,并且我在问自己:为什么要把自己带进这种麻烦中,为什么不用一个DispatcherServlet
加载所有spring配置文件,并完全删除ContextLoaderListener
。(我仍然需要不同的配置文件,但只有一个上下文。)
是否有任何原因不删除ContextLoaderListener
?