如何看Spring源码

2023-05-16

https://blog.csdn.net/qq_27529917/article/details/79209846

想要深入的熟悉了解Spring源码,我觉得第一步就是要有一个能跑起来的极尽简单的框架,下面我就教大家搭建一个最简单的Spring框架,而且是基于Java Config形式的零配置Spring框架。

首先第一步创建一个空的maven web项目,这步很简单,自行百度。

在maven项目的pom.xml文件中添加Spring基础依赖:


        <properties>
            <spring.version>4.3.7.RELEASE</spring.version>
            <slf4j.version>1.7.21</slf4j.version>
            <log4j.version>2.8.2</log4j.version>
            <logging.version>1.2</logging.version>
        </properties>

        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>

        <!-- log配置:Log4j2 + Slf4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency> <!-- 桥接:告诉Slf4j使用Log4j2 -->
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency> <!-- 桥接:告诉commons logging使用Log4j2 -->
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

    </dependencies>
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
73
74
75
76
77
78
79
80
我推荐搭建基于Java Config形式的Spring框架,不需要配置文件,全部使用Java代码形式来定义,简洁明了,对于想要深入了解Spring源码来说这点很重要,否则可能需要看非常多的Spring解析XML配置文件的解析类,对于任何人都不是很容易的功夫。而使用Java Config形式能直接看到配置类的运行流程,对了解Spring源码很有帮助。

要搭建基于Java Config形式的Spring框架,要求Servlet-api版本3.0以上,同时推荐Spring版本4.0以上,日志可以使用我另一篇博客上的log4j2的配置,自己修改下输出日志文件路径就可以了。

public class WebContextInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {RootContextConfig.class};
    }
}

@Configuration
@ComponentScan
public class RootContextConfig {
    @Bean
    public Child child() {
        return new Child();
    }
}

public class Child {
}

@Component
public class Mother {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
把这几个类都放在一个包下,然后启动tomcat,应该就可以启动Spring了,这就是一个最基本的Spring框架,而且包含了Java Config的Bean的定义以及组件扫描,RootContextConfig这个类就是容器的配置类,之后就可以开始跟着Spring框架的启动流程看了,DEBUG跟着一步一步的走。

WebContextInitializer 的父类的将RootContextConfig 当做配置类生成一个AnnotationConfigWebApplicationContext 类型ApplicationContext容器,注入到ContextLoaderListener中。

    ......
    protected WebApplicationContext createRootApplicationContext() {
        Class<?>[] configClasses = getRootConfigClasses();
        if (!ObjectUtils.isEmpty(configClasses)) {
            AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
            rootAppContext.register(configClasses);
            return rootAppContext;
        }
        else {
            return null;
        }
    }
    ......
    protected void registerContextLoaderListener(ServletContext servletContext) {
        WebApplicationContext rootAppContext = createRootApplicationContext();
        if (rootAppContext != null) {
            ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);
            listener.setContextInitializers(getRootApplicationContextInitializers());
            servletContext.addListener(listener);
        }
        else {
            logger.debug("No ContextLoaderListener registered, as " +
                    "createRootApplicationContext() did not return an application context");
        }
    }
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
不要对Tomcat内的源码花时间,主要是看Spring的源码,之后就可以看ContextLoaderListener的contextInitialized(…)方法了,Spring容器就是在这个方法里初始化生成的。如何初始化,这个太复杂了,需要花非常多的时间去看,去思考的,这里就不讲了,不过我可以说一些我自己总结的小技巧:

说是看源码,其实应该叫看和想。Spring源码很复杂,我觉得花在思考上的时间至少要和看的时间对等。看了,如果没有花时间想明白,等于白看。
理解重于记忆。Spring的流程很长,东西很多,如果单纯靠记忆肯定记不过来的,知道每个组件的大体作用以及作用的节点就够了,源码就在那里,又不会跑,记不清了翻翻就看到了,多翻几次就能够慢慢记住了,最开始看的时候不要执着于记忆。
多做笔记。Spring组件很多,在最开始看的时候,推荐做些笔记,将每个重要接口的作用及关键代码记录下,有时间就看看,最先了解的组件是你切入Spring源码的切口,借助他们能关联到其他的组件。
多百度。在看一些关键接口或者类时,如果其代码很复杂,先百度下吧,先对其功能有个了解,然后对照着功能看代码会有很大的帮助。
要多遍地看,反复地看。别想着看一遍就能看明白,在最开始的几次跟着初始化流程看源码时,不要执着于某个细节。先对Spring所有的组件功能有个大体了解,对初始化流程有个大体的了解,这是深入的基础。
多看日志,多DEBUG。多看日志能够提高你对Spring流程的了解程度,而且在排错时能有效提高效率;DEBUG是看源码最好的一种方式,是解疑的最直接途径,对于有些运行时难以触及的代码,需要你手动创造条件让流程走入此处。
多总结,多动手。不要仅局限于看和思考,要动手。源码看的仔细,基本能从源码上看出很多Spring组件的使用方式,总结各种组件的使用方法,然后自己定义相应的组件,将其引入Spring中,查看其作用流程,这是你拓展Spring的第一步,既能增强对Spring的理解,也能提高你对Spring的拓展能力。
不断完善框架。每熟悉一种组件的使用,就添加到自己的 框架中,不断完善自己的框架,到最后你的框架能够整合Spring大部分好用组件,也是你以后回顾Spring框架的最大助力。
多看注释,看方法的名称,参数和返回值。看注释,理解类,属性和方法的作用,着重第一段注释;看方法的名称,参数和返回值能对方法的作用有很明显的说明。
以上就是我自己看Spring总结的一些小技巧,希望对你们有些助益。如果在看Spring源码的过程中有些疑问,可以在回复里提,我会尽量帮助解答。
--------------------- 
作者:jjb_hz 
来源:CSDN 
原文:https://blog.csdn.net/qq_27529917/article/details/79209846 
版权声明:本文为博主原创文章,转载请附上博文链接!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何看Spring源码 的相关文章

  • 解释内存中的栈(stack)、堆(heap)和静态区(static area)的用法。

    答 xff1a 通常我们定义一个基本数据类型的变量 xff0c 一个对象的引用 xff0c 还有就是函数调用的现场保存都使用内存中的栈空间 xff1b 而通过new关键字和构造器创建的对象放在堆空间 xff1b 程序中的字面量 xff08
  • Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?

    Math round 11 5 的返回值是12 xff0c Math round 11 5 的返回值是 11 四舍五入的原理是在参数上加0 5然后进行下取整 所谓向上取整指当计算的结果不为整数时取大于计算结果的整数 向下取整指当计算的结果不
  • Spring注入详解

    注入方式 构造函数注入 public class UserServiceImpl implents UserService private UserDao userDao 64 Autowire public UserServiceImpl
  • yml文件

    YAML文件简介 我们可能在spring配置文件里见到过 yml格式的东东 xff0c 配置文件不都是 propertie或者 xml文件吗 xff1f yml是什么鬼 xff0c 今天我带你们来一探究竟 YAML xff08 Yet An
  • YAML 语言教程

    作者 xff1a 阮一峰 日期 xff1a 2016年7月 4日 编程免不了要写配置文件 xff0c 怎么写配置也是一门学问 YAML 是专门用来写配置文件的语言 xff0c 非常简洁和强大 xff0c 远比 JSON 格式方便 本文介绍
  • 如何解决idea的Could not autowire. No beans of 'xxxx' type found

    打开设置setting 在左侧找到Editor xff0c 然后选择 Inspections 在右侧的搜索框下面 xff0c 找到SPRING那块 xff0c 然后找到spring的核心包 xff0c 选中spring core 找到cod
  • IntelliJ IDEA中绿色注释扫描飘红报错解决

    在IDEA中的setting中搜索 34 javadoc 34 基本上 xff0c 绿色注释飘红的问题是解决了 xff1b
  • Windows10在当前目录快速打开cmd的方法

    1 按住Shift键 xff0c 鼠标右键快捷方式 xff0c 先打开Powershell窗口 2 输入 start cmd 回车 3 这样就可以打开cmd窗口了 xff0c 并且cmd的工作目录就是当前的目录
  • marvn 环境变量配置

    1 首先下载maven xff0c 下载地址 xff1a http maven apache org download html 打开这个连接 xff1a 选择File下面的apache maven 3 2 1 bin zip链接进行下载
  • mvn命令

    在pom xml目录下 打开cmd xff0c 输入mvn命令 1 mvn dependency tree 打印项目的依赖树到控制台 mvn dependency tree gt gt D tree txt 导出依赖树到指定文件 2 mvn
  • cmd命令

    1 创建多级目录 md mkdir 目录1 目录2 目录3 C Users xxx gt pushd d D gt md 1 2 3 4 D gt pushd D 1 2 3 4 D 1 2 3 4 gt 2 pushd POPD push
  • css浏览器兼容问题

    1 CSS中几种浏览器对不同关键字的支持 xff0c 可进行浏览器兼容性重复定义 important 可被FireFox和IE7识别 可被IE6 IE7识别 可被IE6识别 43 可被IE7识别 区别IE6与FF xff1a backgro
  • mybatis annotations

    1 Alias别名 64 Documented 64 Retention RetentionPolicy RUNTIME 64 Target ElementType TYPE public 64 interface Alias String
  • Python+Flask实现股价查询系统。Python绘制股票k线走势

    文章目录 一 实现效果图二 实现思路1 获取数据 2 可视化数据三 源码获取 一 实现效果图 打开默认显示半年线 xff0c 可以通过可视化类型选择可视化k线图 高低点等 xff08 目前只完成了初版 xff0c 当查询的股票数据返回为空时
  • typeHandlers 类型处理器

    类型转换器官网地址 无论是 MyBatis 在预处理语句 xff08 PreparedStatement xff09 中设置一个参数时 xff0c 还是从结果集中取出一个值时 xff0c 都会用类型处理器将获取的值以合适的方式转换成 Jav
  • idea创建父子工程

    new 一个project xff0c 删除src xff0c 只保留pom文件 xff0c 作为主工程 webparent xff0c 工程目录D ideaProjects self multimodule xff1b 右键点击上面创建的
  • css基础

    层叠次序 当同一个 HTML 元素被不止一个样式定义时 xff0c 会使用哪个样式呢 xff1f 一般而言 xff0c 所有的样式会根据下面的规则层叠于一个新的虚拟样式表中 xff0c 其中数字 4 拥有最高的优先权 浏览器缺省设置外部样式
  • spring官网下载jar包

    http repo spring io release org springframework spring 查找方法 xff1a https spring io gt 点击 project https spring io projects
  • idea快捷键

    Intellij IDEA神器居然还藏着这些实用小技巧 xff0c 爽 xff01 xff01 xff01 自动补全返回值 可以引入变量 ctrl 43 alt 43 v Ctrl 43 或者 xff0c 可以跑到大括号的开头与结尾 Ctr
  • IDEA添加serialVersionUID

    打开IDEA中的 Setting gt Editor gt Inspections 选项中 xff0c java gt Serialization issues gt 将Serializable class without 39 seria

随机推荐