81,092
社区成员
发帖
与我相关
我的任务
分享
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
http://www.codesenior.com/en/tutorial/Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
[quote=引用 1 楼 Inhibitory 的回复:] 1. 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。 2. 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。 3. 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,因为父子 Context 的可见性问题,防止重复加载所以在 listener 里加载。 所以,有时可用可不用,有时必用,具体看情况。
估计你们项目里没和Ioc容器一起使用。 一般配置spring监听器都是为了配合Ioc容器一起使用的。 spring源代码里可以看到,启动web服务器的时候会调用监听器的上下文初始化方法,然后去初始化Ioc容器。
1. 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。 2. 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。 3. 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,因为父子 Context 的可见性问题,防止重复加载所以在 listener 里加载。 所以,有时可用可不用,有时必用,具体看情况。
1. 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。 2. 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。 3. 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,因为父子 Context 的可见性问题,防止重复加载所以在 listener 里加载。 所以,有时可用可不用,有时必用,具体看情况。