A. 怎么阅读Spring源码
准备工作
1. 安装github:现在spring源代码都在github管理,所以首先需要下载githup,下;
2. 安装gradle构建工具: 下载完后进行解压到任意盘符,然后增加环境变量GRADLE_HOME,并在环境变量bin中增加%GRADLE_HOME%/bin,打开DOS窗口,运行gradle -v,出现版本号等信息,表示安装成功;
3. 下载Spring源码:首先打开git shell,切换到你的工作目录,然后输入以下命令:git clone git://github.com/SpringSource/Spring-framework.git,后面一串是源码下载地址。大概半小时的样子,就可以下载完成,这时候在你的工作目录中就会出现Spring-framework的目录,里面有Spring各组件的源码包;
4. 构建导入:下载下来的代码不能直接导入Eclipse,要先转换成Eclipse能读取的形式。因为所有组件都会依赖spring-core,所有我们首先要转换Spring-core工程,在命令窗口切换到Spring-core工程,运行gradle cleanidea eclipse命令,我们会看到开始下载工程所依赖的jar包,几分钟后执行完毕,再来看Spring-core文件夹,多了.classpath、.project等文件,这是Eclipse工程所必须的,然后可以把他导入到eclipse。因为大部分Spring组件都会用到 spring-beans、spring-context、spring-aop,而他们又依赖spring-expression、spring-instrument,所以我们干脆先把这些工程都进行转换并导入eclipse。
我初次导入过程并不顺利,拿spring-core为例,其中以来的一个jar包是Spring-framework/spring-core/build/libs/spring-asm-repack-4.0.jar,但我工程里面并没有他,只好在网上下载了一个,并加入构建路径,其次我还发现少commons-pool-1.5.3.jar、spring-cglib-repack-3.0.jar,都一一下载,最后还是报错没有java.util.concurrent.ForkJoinPool类,发现这个版本必须使用jdk1.7以上,1.6没有这个包。折腾半天,终于几个工程没变异错误了,向前迈进了一步。
B. spring的源码jar怎么使用
首先,在工程右键,属性中,添加必要的jar包。
选中必要的jar包,上面给出的源码jar包中,导入spring3.0.5中的所有jar包。
其中lib内的是spring的jar包,用到哪个导入哪个,不知道的话,全部导入就行了。
外面的几个jar包,用于日志以及mysql的驱动。commons-logging jar包是必须的,其他的随意吧。
不确定的话,lib外面的这几个jar包以及lib里面的都导入就行了。
C. 阿里内部都在用的Spring源码手册,学会也是阿里人
最近在使用Spring MVC过程中遇到了一些问题,网上搜索不少帖子后虽然找到了答案和解决方法,但这些答案大部分都只是给了结论,并没有说明具体原因,感觉总是有点不太满意。
更重要的是这些所谓的结论大多是抄来抄去,基本源自一家,真实性也有待考证。
那作为程序员怎么能知其所以然呢?
此处请大家内心默读三遍。
阅读源码的魅力在于:
分享一本阿里内部人都在使用的Spring源码手册分享给读者朋友们,学会掌握了本手册内容,距离成为阿里人也是成功的跨了一大步子。
第一部分:核心实现原理
第二部分:企业应用
D. 怎么阅读spring源码
从HttpServletBean的init()进入,再到initWebApplicationContext(),再到refresh(),再到refreshBeanFactory(),再到finishRefresh(),直到服务器启动成功。不知道读了多少遍,
但是源码的东西实在的太多了,想要完全读懂,完全理清头绪,还差很远啊。所以我只重点关注了两块内容,就是bean的定位加载解析注册、bean的实例化两大块内容,其他地方稍作了解,没有太过深入。
整个容器的启动流程,都在AbstractApplicationContext的refresh()的模板方法中了。
复制代码
1 public void refresh() throws BeansException, IllegalStateException {
2 synchronized (this.startupShutdownMonitor) {
3 // Prepare this context for refreshing.
4 prepareRefresh();
5
6 // Tell the subclass to refresh the internal bean factory.
7 beanFactory = obtainFreshBeanFactory();
8
9 // Prepare the bean factory for use in this context.
10 prepareBeanFactory(beanFactory);
11
12 try {
13 // Allows post-processing of the bean factory in context subclasses.
14 postProcessBeanFactory(beanFactory);
15
16 // Invoke factory processors registered as beans in the context.
17 (beanFactory);
18
19 // Register bean processors that intercept bean creation.
20 registerBeanPostProcessors(beanFactory);
21
22 // Initialize message source for this context.
23 initMessageSource();
24
25 // Initialize event multicaster for this context.
26 ();
27
28 // Initialize other special beans in specific context subclasses.
29 onRefresh();
30
31 // Check for listener beans and register them.
32 registerListeners();
33
34 // Instantiate all remaining (non-lazy-init) singletons.
35 (beanFactory);
36
37 // Last step: publish corresponding event.
38 finishRefresh();
39 }
40
41 catch (BeansException ex) {
42 // Destroy already created singletons to avoid dangling resources.
43 destroyBeans();
44
45 // Reset 'active' flag.
46 cancelRefresh(ex);
47
48 // Propagate exception to caller.
49 throw ex;
50 }
51 }
52 }
其实,我并没有上来就看源码,而是先从看书开始,稍微了解,知道了一些关键点,关键流程,自己产生了一堆疑问,然后带着疑问去读源码,读着读着,发现有些疑问就这么解决了。