Spring中DispatcherServlet与ContextLoaderListener的区别

昨天在写springmvc的时候,在web.xml中配置了DispatcherServlet,如下:

	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

 想到以前写ssh的时候,web.xml中配置的都是listener,如下:

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

 他们都是去加载spring的配置文件,那么他们有什么不同呢?后来在网上查了下资料,发现他们的作用域是不同的,在dispatcherServlet中加载的配置文件的作用域是在web请求的时候才触发的,一般跟controller相关的bean配置会放在这里面,例如视图解析器的bean,而ContextLoaderListener加载的配置文件的作用域是在整个环境中,类似于application级别的,一般在这个配置文件中定义的是公共的内容,例如db、service等等。写的很浅,欢迎拍砖....

猜你喜欢

转载自yonge812.iteye.com/blog/1535488