Spring源码详解
AOP
XML注册
@Aspect注册
- contextInitialized:103, ContextLoaderListener
- initWebApplicationContext:291, ContextLoader
- configureAndRefreshWebApplicationContext:400, ContextLoader
- refresh:549, AbstractApplicationContext
- finishBeanFactoryInitialization:877, AbstractApplicationContext
- preInstantiateSingletons:830, DefaultListableBeanFactory
- getBean:199, AbstractBeanFactory
- doGetBean:318, AbstractBeanFactory
- getSingleton:222, DefaultSingletonBeanRegistry
- getObject:-1, 889007824
- lambda$doGetBean$0:320, AbstractBeanFactory
- createBean:515, AbstractAutowireCapableBeanFactory
- doCreateBean:593, AbstractAutowireCapableBeanFactory
- initializeBean:1766, AbstractAutowireCapableBeanFactory
- applyBeanPostProcessorsAfterInitialization:429, AbstractAutowireCapableBeanFactory
- postProcessAfterInitialization:296, AbstractAutoProxyCreator
- wrapIfNecessary:335, AbstractAutoProxyCreator
- createProxy:471, AbstractAutoProxyCreator
- createProxy:471, AbstractAutoProxyCreator
- getProxy:160, CglibAopProxy
1 | public Object getProxy(@Nullable ClassLoader classLoader) { |
1 | // Create proxy if we have advice. |
执行
- intercept:688, CglibAopProxy$DynamicAdvisedInterceptor
- proceed:162, ReflectiveMethodInvocation
1 | public Object proceed() throws Throwable { |
处理请求
注册
- refresh:549, AbstractApplicationContext
- finishBeanFactoryInitialization:877, AbstractApplicationContext
- preInstantiateSingletons:849, DefaultListableBeanFactory
- getBean:199, AbstractBeanFactory
- doGetBean:318, AbstractBeanFactory
- getSingleton:222, DefaultSingletonBeanRegistry
- getObject:-1, 911637678
- lambda$doGetBean$0:320, AbstractBeanFactory
- createBean:515, AbstractAutowireCapableBeanFactory
- doCreateBean:593, AbstractAutowireCapableBeanFactory
- initializeBean:1758, AbstractAutowireCapableBeanFactory
- invokeInitMethods:1821, AbstractAutowireCapableBeanFactory
- afterPropertiesSet:164, RequestMappingHandlerMapping
- afterPropertiesSet:199, AbstractHandlerMethodMapping
- initHandlerMethods:211, AbstractHandlerMethodMapping
getCandidateBeanNames()获取到需要初始化的bean,processCandidateBean(beanName)方法对这些bean进行过滤
1 | protected void initHandlerMethods() { |
- processCandidateBean:250, AbstractHandlerMethodMapping
isHandler(beanType)主要过滤条件,detectHandlerMethods(beanName);注册
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15protected void processCandidateBean(String beanName) {
Class<?> beanType = null;
try {
beanType = obtainApplicationContext().getType(beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isTraceEnabled()) {
logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
}
}
if (beanType != null && isHandler(beanType)) {
detectHandlerMethods(beanName);
}
}符合@Controller或者@RequestMapping
1
2
3
4protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
请求
- org.springframework.web.servlet.DispatcherServlet
- service:882, FrameworkServlet
- service:742, HttpServlet
- service:635, HttpServlet
这个方法具体判断是什么请求类型
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
58protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < lastModified) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
} - doGet:897, FrameworkServlet
- processRequest:1005, FrameworkServlet
- doService:942, DispatcherServlet
- doDispatch:1014, DispatcherServlet
- getHandler:1231, DispatcherServlet
- getHandler:401, AbstractHandlerMapping
- getHandlerInternal:365, AbstractHandlerMethodMapping
AbstractHandlerMethodMapping内部使用了读写锁
lookupHandlerMethod(lookupPath, request); 从注册好的集合中查找返回HandlerMethod调用
mappingRegistry.urlLookup这个map保存了url和对应的方法
1
2
3
4
5
6
7
8
9
10
11protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
this.mappingRegistry.acquireReadLock();
try {
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);
}
finally {
this.mappingRegistry.releaseReadLock();
}
}
事务
Spring并不直接管理事务,而是提供了事务管理接口是PlatformTransactionManager,通过这个 接口,Spring 为各个平台如JDBC、Hibernate等都提供了对应的事务管理器,但是具体的实现就是各个平台自己的 事情了。
平台 | 实现类 |
---|---|
JDBC | DataSourceTransactionManager |
JTA | JtaTransactionManager |
Hibernate | HibernateTransactionManager |