1.spring容器启动时:
(1.)首先读取web.xml文件,通过配置ContextLoaderListener监听容器启动时 创建application对象;
(2.)ContextLoaderListener读取contextConfigLocation指定的beanx.xml文件,加载配置;
(3.)<context:component-scan base-package="com.example" /> 配置spring包扫描,扫描com.example下的所有包中的spring注解。通过spring框架的ioc控制反转和di依赖注入(dao层的@Repository,biz层的@Service,@Controller,@Resource等);
(4.)<mvc:annotation-driven /> 配置spring mvc框架的驱动,开启spring mvc的注解解析 @RequestMapping("/aa.action") 响应/aa.action请求。
2.处理请求时:
(1.)配置的characterEncodingFilter过滤器会过滤编码集为utf-8的请求;
(2.)HiddenHttpMethodFilter过滤Http请求为restful方法(_method=delete、put等);
(3.)如果请求的是*.action,<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 由spring mvc处理;
(4.)DispatcherServlet解析出请求后,匹配@RequestMapping,传参过程由spring mvc注解参数到参数对象;
(5.)@Controller 调用业务层 @Resource(name="***Impl"),业务层调用dao层,以此来实现对数据库的操作;
(6.)若有需要,返回JsonModel(@RestController),转为json字符串给客户端;
3.spring mvc的功能:
(1.)拦截请求;
(2.)解析参数;
(3.)由注解注入request,session...;
(4.)形成视图并且将数据写入视图;
(5.)Controller是一个普通的javabean,不需要依赖于j2ee容器,便于测试(mock)
转载请注明原文地址: https://www.6miu.com/read-24236.html