DispatcherServlet和ContextLoaderListener是什么?
20180509 check
接收到HTTP请求后,DispatcherServlet 询问HandlerMapping (configuration files) 去请求合适的Controller。Controller接受请求后,调用合适的service方法,然后set model data,接下来返回视图(view)给DispatcherServlet。DispatcherServlet从ViewResolver 选择定义好的视图给request请求。一旦视图确定下来,DispatcherServlet 传递model data给最终渲染到浏览器上的视图。
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
ContextLoaderListener 读取spring配置文件(“contextConfigLocation” in web.xml),解析它、加载定义在配置文件中的beans。
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring-dao-hibernate.xml,
WEB-INF/spring-services.xml,
WEB-INF/spring-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
注:也可以将多个context files放到一个已经存在的配置文件中
<beans>
<import resource="spring-dao-hibernate.xml"/>
<import resource="spring-services.xml"/>
<import resource="spring-security.xml"/>
... //Other configuration stuff
</beans>
<context:annotation-config> = Scanning and activating annotations in “already registered beans”.
<context:component-scan> = Bean Registration + Scanning and activating annotations
浙公网安备 33010602011771号