Spring(Spring MVC):Part one
2010-11-07 00:39
387 查看
I think read open source code is a good way tolearn programming skill.DI is very important in OO programming,and Spring is a very power IoC container in Java world.So I choose Spring to learn.
If you want to read a software’s code,you should start with it’s entry point.I think may be Spring MVC is a entry point for Spring.So we can start with Spring MVC,and then go into Spring internal.
Spring MVC is a MVC framework,and it’s base on Spring IoC container,so it’s very flexible.You can write like this to create a controller:
[/code]
That’s very clear,just some annotations from Spring,not introduce any additional classes,you don’t need extend any base class.You can reuse this code anywhere if you want.
As early mentioned,we should find a entry point when we read code.In Spring MVC,the org.springframework.web.servlet.DispatcherServlet is the entry point,so we read it first.
If you develop a Spring MVC application,you must config the DispatcherServlet in the web.xml like this:
[/code]
DispatcherServlet is a standard HttpServlet.DispatcherServlet inherit from FrameworkServlet,FrameworkServlet inherit from HttpServletBean,and HttpServletBean inherit from HttpServlet.
You can find a init() method in HttpServletBean,this is the java HttpSerlvet initlization method.This method will call two virtual methods,these two methods will be implemented by FrameworkServlet.FrameworkSevlet will initlization WebApplicationContext(Spring context),and delegate doGet,doPost,doPut,doDelete,doOptions to doService.doService is an abstract method,so FrameworkServlet’s subclass must implement this method.
In the FrameworkServlet’s initServletBean method(override method),call initWebApplicationContext method and iniFrameworkServlet method.initWebApplicationContext will call onRefresh method.onRefresh is a virtual method,it will be implemented by DispatcherServlet.
I will introduce the DispatcherServlet in next article detail.
Thanks~
If you want to read a software’s code,you should start with it’s entry point.I think may be Spring MVC is a entry point for Spring.So we can start with Spring MVC,and then go into Spring internal.
Spring MVC is a MVC framework,and it’s base on Spring IoC container,so it’s very flexible.You can write like this to create a controller:
[code] @Controller
@RequestMapping("home")
public class HomeController
{
@RequestMapping("/index")
public ModelAndView index()
{
return new ModelAndView("index");
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public ModelAndView login(User user)
{
//do something
return new ModelAndView(...);
}
}
[/code]
That’s very clear,just some annotations from Spring,not introduce any additional classes,you don’t need extend any base class.You can reuse this code anywhere if you want.
As early mentioned,we should find a entry point when we read code.In Spring MVC,the org.springframework.web.servlet.DispatcherServlet is the entry point,so we read it first.
If you develop a Spring MVC application,you must config the DispatcherServlet in the web.xml like this:
[code] <servlet>
<servlet-name>book</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
spring-book-servlet.xml
</param-value>
</init-param>
</servlet>
[/code]
DispatcherServlet is a standard HttpServlet.DispatcherServlet inherit from FrameworkServlet,FrameworkServlet inherit from HttpServletBean,and HttpServletBean inherit from HttpServlet.
You can find a init() method in HttpServletBean,this is the java HttpSerlvet initlization method.This method will call two virtual methods,these two methods will be implemented by FrameworkServlet.FrameworkSevlet will initlization WebApplicationContext(Spring context),and delegate doGet,doPost,doPut,doDelete,doOptions to doService.doService is an abstract method,so FrameworkServlet’s subclass must implement this method.
In the FrameworkServlet’s initServletBean method(override method),call initWebApplicationContext method and iniFrameworkServlet method.initWebApplicationContext will call onRefresh method.onRefresh is a virtual method,it will be implemented by DispatcherServlet.
I will introduce the DispatcherServlet in next article detail.
Thanks~
相关文章推荐
- Spring入门学习之Spring MVC(part 1)
- WebService analyze part one : build wsdl from java service .
- spring笔记4 spring MVC的基础知识4
- [置顶] Spring、Spring MVC、MyBatis整合文件配置详解
- spring aop 拦截spring mvc controller
- Spring、Spring MVC、MyBatis整合文件配置详解
- Part II. What’s New in Spring Framework 4.x
- Spring 系列,第 3 部分: 进入 Spring MVC
- Spring学习日志之Spring MVC启动配置
- Spring mvc(2)spring mvc+quarz+mybatis+druid
- spring MVC总结(核心流程图、如何访问到静态的文件、Spring中的拦截器、全局异常、JUnit单元测试、ajax请求)
- 如何 在Spring MVC中 使用多个Spring和MyBatis的xml配置文件(多模块配置)
- 纯函数式堆(纯函数式优先级队列)part one ----二项堆
- jee、spring、spring mvc、mybatis 学习(六)
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
- Testing SQL queries with Spring and DbUnit, part 1
- Spring MVC 使用问题与解决--HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
- Maven+Spring 4+Spring MVC(一)
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(新手教程)
- Spring MVC 零配置 / Spring MVC JavaConfig