将ContextLoaderListener添加到Spring MVC中的web.xml

我是Spring MVC的新手。 我有一个Web应用程序。 我有以下配置:

 list.html   spring  org.springframework.web.servlet.DispatcherServlet  1   spring /  

我是否需要将以下行添加到web.xml文件中?

   org.springframework.web.context.ContextLoaderListener   

是的,您需要在web.xml添加ContextLoaderListener ,仅您在加载应用程序时还要加载其他Spring上下文xml文件时,您可以将它们指定为

  contextConfigLocation  /WEB-INF/spring-security.xml   

仅当您有两个config xml文件时。 一个是Services / DAO,另一个是Controller。 如果您在一个spring配置文件中配置了所有内容,则不需要ContextLoaderListener ,只需调度程序servlet即可。

建议将config拆分为两个,并使用ContextLoaderListener创建根应用程序上下文和调度程序servlet以创建Web层应用程序上下文。

它是可选的,你真的不需要它只是为了Spring MVC( DispatcherServlet会这样做)。 但是必须将Spring安全性添加到Spring MVC中

  org.springframework.web.context.ContextLoaderListener  

只需注意一句,如果使用ContextLoaderListener ,则必须添加DelegatingFilterProxy

   org.springframework.web.context.ContextLoaderListener    springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy   springSecurityFilterChain /admin   contextConfigLocation  /WEB-INF/spring-security.xml   

在你的web.xml中也是如此。 抱歉四年太晚了。 干杯

  spring  org.springframework.web.servlet.DispatcherServlet   contextConfigLocation classpath:applicationContext.xml,WEB-INF/spring-security.xml  1   spring UR_PATTERN  

这对我来说很好。