17.SpringMVC源码分析

 

1.服务器启动时的源码分析

  1. 寻找init()

  2. DispatcherServlet类中没有init(),在它的父类中寻找init()

  3. FrameworkServlet类中没有init(),在它的父类中寻找init()

  4. HttpServletBean.init()

  5. HttpServletBean.init() –> HttpServletBean.initServletBean(),但是HttpServletBean.initServletBean()方法体为空,找它的子类对initServletBean()的实现

  6. FrameworkServlet.initServletBean() –> FrameworkServlet.initWebApplicationContext()

  7. FrameworkServlet.initWebApplicationContext() –> FrameworkServlet.onRefresh(),但是FrameworkServlet.onRefresh()方法体为空,找它的子类对onRefresh()的实现

  8. DispatcherServlet.onRefresh() –> DispatcherServlet.initStrategies()

  9. DispatcherServlet.initStrategies()调用了initHandlerMappings(), initHandlerAdapters(), initViewResolvers()等方法进行了初始化

DispatcherServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DispatcherServlet extends FrameworkServlet {
// ...

@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}

protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}

// ...
}

FrameworkServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
// ...

@Override
protected final void initServletBean() throws ServletException {
// ...
try {
this.webApplicationContext = initWebApplicationContext();
// ...
}
// ...
}

protected WebApplicationContext initWebApplicationContext() {
// ...

if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
synchronized (this.onRefreshMonitor) {
onRefresh(wac);
}
}

// ...
}

protected void onRefresh(ApplicationContext context) {
// For subclasses: do nothing by default.
}

// ...
}

HttpServletBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware {
// ...

@Override
public final void init() throws ServletException {
// ...

// Let subclasses do whatever initialization they like.
initServletBean();
}

protected void initServletBean() throws ServletException {

}

// ...
}

2.当接收用户请求时的源码分析

  1. 寻找service()

  2. DispatcherServlet类中没有service(),在它的父类中寻找service()

  3. FrameworkServlet.service()

  4. FrameworkServlet.service() –> FrameworkServlet.processRequest()

  5. FrameworkServlet.processRequest() –> FrameworkServlet.doService(),但是FrameworkServlet.doService()方法是一个抽象方法,找它的子类对doService()的实现

  6. DispatcherServlet.doService() –> DispatcherServlet.doDispatch()

  7. DispatcherServlet.doDispatch() –> org.springframework.web.servlet.HandlerAdapter.handle()DispatcherServlet.processDispatchResult()

  8. DispatcherServlet.processDispatchResult() –> DispatcherServlet.render()

  9. DispatcherServlet.render() –> org.springframework.web.servlet.View.render()

FrameworkServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
// ...

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request, response);
}
else {
super.service(request, response);
}
}

protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// ...

try {
doService(request, response);
}

// ...
}

protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
throws Exception;

// ...
}

DispatcherServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class DispatcherServlet extends FrameworkServlet {
// ...

@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
// ...

try {
doDispatch(request, response);
}

// ...
}

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
// ...

try {
ModelAndView mv = null;
// ...

try {
// ...

// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

// ...

// Actually invoke the handler.
// 执行Controller里面的方法
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

// ...
}
// ...
// 转发和渲染
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
// ...
}

private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
@Nullable Exception exception) throws Exception {

// ...

// Did the handler return a view to render?
if (mv != null && !mv.wasCleared()) {
// 渲染
render(mv, request, response);
// ...
}

// ...
}

protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
// ...

try {
// ...
// 渲染
view.render(mv.getModelInternal(), request, response);
}

// ...
}

// ...
}