Spring源码阅读目录
第一部分——IOC篇
第一章 Spring之最熟悉的陌生人——IOC
第二章 Spring之假如让你来写IOC容器——加载资源篇
第三章 Spring之假如让你来写IOC容器——解析配置文件篇
第四章 Spring之假如让你来写IOC容器——XML配置文件篇
第五章 Spring之假如让你来写IOC容器——BeanFactory和FactoryBean
第六章 Spring之假如让你来写IOC容器——Scope和属性填充
第七章 Spring之假如让你来写IOC容器——属性填充特别篇:SpEL表达式
第八章 Spring之假如让你来写IOC容器——拓展篇
第九章 Spring之源码阅读——环境搭建篇
第十章 Spring之源码阅读——IOC篇
第二部分——AOP篇
第十一章 Spring之不太熟的熟人——AOP
第十二章 Spring之不得不了解的内容——概念篇
第十三章 Spring之假如让你来写AOP——AOP联盟篇
第十四章 Spring之假如让你来写AOP——雏形篇
第十五章 Spring之假如让你来写AOP——Joinpoint(连接点)篇
第十六章 Spring之假如让你来写AOP——Pointcut(切点)篇
第十七章 Spring之假如让你来写AOP——Advice(通知)上篇
第十八章 Spring之假如让你来写AOP——Advice(通知)下篇
第十九章 Spring之假如让你来写AOP——番外篇:Spring早期设计
第二十章 Spring之假如让你来写AOP——Aspect(切面)篇
第二十一章 Spring之假如让你来写AOP——Weaver(织入器)篇
第二十二章 Spring之假如让你来写AOP——Target Object(目标对象)篇
第二十三章 Spring之假如让你来写AOP——融入IOC容器篇
第二十四章 Spring之源码阅读——AOP篇
第三部分——事务篇
第二十五章 Spring之曾经的老朋友——事务
第二十六章 Spring之假如让你来写事务——初稿篇
第二十七章 Spring之假如让你来写事务——铁三角篇
第二十八章 Spring之假如让你来写事务——属性篇
第二十九章 Spring之假如让你来写事务——状态篇
第三十章 Spring之假如让你来写事务——管理篇
第三十一章 Spring之假如让你来写事务——融入IOC容器篇
第三十二章 Spring之源码阅读——事务篇
第四部分——MVC篇
第三十三章 Spring之梦开始的地方——MVC
第三十四章 Spring之假如让你来写MVC——草图篇
第三十五章 Spring之假如让你来写MVC——映射器篇
第三十六章 Spring之假如让你来写MVC——拦截器篇
第三十七章 Spring之假如让你来写MVC——控制器篇
第三十八章 Spring之假如让你来写MVC——适配器篇
第三十九章 Spring之假如让你来写MVC——番外篇:类型转换
第四十章 Spring之假如让你来写MVC——ModelAndView篇
第四十一章 Spring之假如让你来写MVC——番外篇:数据绑定
第四十二章 Spring之假如让你来写MVC——视图篇
第四十三章 Spring之假如让你来写MVC——上传文件篇
第四十四章 Spring之假如让你来写MVC——异常处理器篇
第四十五章 Spring之假如让你来写MVC——国际化篇
第四十六章 Spring之假如让你来写MVC——主题解析器篇
第四十七章 Spring之假如让你来写MVC——闪存管理器篇
第四十八章 Spring之假如让你来写MVC——请求映射视图篇
第四十九章 Spring之假如让你来写MVC——番外篇:属性操作
第五十章 Spring之假如让你来写MVC——融入IOC容器篇
第五十一章 Spring之源码阅读——MVC篇
第五部分——Boot篇
第五十二章 Spring之再进一步——Boot
第五十三章 Spring之假如让你来写Boot——环境篇
第五十四章 Spring之假如让你来写Boot——注解篇(上)
第五十五章 Spring之假如让你来写Boot——注解篇(下)
第五十六章 Spring之假如让你来写Boot——SPI篇
第五十七章 Spring之假如让你来写Boot——配置文件篇(上)
第五十八章 Spring之假如让你来写Boot——配置文件篇(下)
第五十九章 Spring之假如让你来写Boot——番外篇:再谈Bean定义
第六十章 Spring之假如让你来写Boot——自动装配篇
第六十一章 Spring之假如让你来写Boot——番外篇:杂谈Starter
第六十二章 Spring之假如让你来写Boot——番外篇:重构BeanFactory
第六十三章 Spring之假如让你来写Boot——番外篇:再谈ApplicationContext
第六十四章 Spring之假如让你来写Boot——内嵌Web容器篇
第六十五章 Spring之假如让你来写Boot——Main方法启动篇
第六十六章 Spring之最终章——结语篇
文章目录
前言
对于Spring一直都是既熟悉又陌生,说对它熟悉吧,平时用用没啥问题,但面试的时候被问的一脸懵逼,就很尴尬,都不好意思在简历上写着熟悉Spring了
所以决定花点时间研究研究Spring的源码。主要参考的书籍是:《Spring源码深度解析(第2版)》、《Spring揭秘》、《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》
什么是IOC
一说到Spring,不论面试还是平时,想必大家接触最多的就是IOC和AOP。之前为了面试,我也背过八股文,不过都是流于表面,面试时跟念经一样说出来,并没有对其有太多的理解,面试官一深问就要回家等电话了。接下来就先一起来看看IOC是何方神圣吧
说到IOC,相信很多同学或多或少都对其有所了解
IOC全称是Inversion of Control,控制反转。它是一种设计思想,由容器将设计好的对象交给容器控制,而非对象内部直接创建…
巴拉巴拉。。。 这些都是耳熟能详,老生常谈了,想必大家都看得都想吐了,这里就不给大伙儿添堵了
IOC其实就是原本由开发创建的对象交由容器创建,而开发只要在需要的时候去获取就行。为啥要交给容器创建,这样有什么好处吗?答案是显而易见的,设想下这么个场景:A需要用B的某个功能,而B又依赖于C、D、E,这样A去创建B的时候,就需要同时知道C、D、E,这样对于模块之间其实是耦合的,我只想用B的功能而已,C、D、E关我啥事?
IOC与传统方式的区别
文字还是太过于枯燥,举个例子:假设现在有个电脑的类,电脑必然包括CPU,显卡,磁盘,内存这些东西,所以电脑类的定义如下:
package com.hqd.test.ch02.bean;
public class Computer {
private CPU cpu;
private GPU gpu;
private Disk disk;
private Memory memory;
public Computer() {
}
public Computer(CPU cpu, GPU gpu, Disk disk, Memory memory) {
this.cpu = cpu;
this.gpu = gpu;
this.disk = disk;
this.memory = memory;
}
public void powerOn() {
System.out.println(cpu);
System.out.println(gpu);
System.out.println(disk);
System.out.println(memory);
System.out.println("启动完毕 ####################");
}
public void powerOff() {
System.out.println("正在关机 ####################");
}
public CPU getCpu() {
return cpu;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public GPU getGpu() {
return gpu;
}
public void setGpu(GPU gpu) {
this.gpu = gpu;
}
public Disk getDisk() {
return disk;
}
public void setDisk(Disk disk) {
this.disk = disk;
}
public Memory getMemory() {
return memory;
}
public void setMemory(Memory memory) {
this.memory = memory;
}
}
一、传统方式
在用Spring之前,不妨先看看传统方式是如何创建依赖对象的,测试代码如下:
@Test
public void tradition() {
Computer computer = new Computer();
computer.setCpu(new CPU());
computer.setGpu(new GPU());
computer.setDisk(new Disk());
computer.setMemory(new Memory());
computer.powerOn();
}
我只是想开个机而已,但是却要知道CPU什么的,这还只是空构造器,如果在加上几个构造参数:规格,频率、产商等。。 想想都恐怖,这对用使用者是多么不友好的一件事啊!如果现在有个玩意,只要我对他说,我想要个电脑,他就把电脑给我,我只要按下开机就行了,这该多好啊
二、IOC方式
噔噔噔!IOC终于闪亮登场了,它不再让使用者关注对象内部的创建细节,只要跟它说需要个对象(电脑),他会给我们,我们也不在关注依赖对象内部(CPU、内存等)的变化,也就是模块和模块之间解耦了,模块A的变化再也不会影响到模块B了
看下IOC是如何实现的,首先我们需要新建XML文件,如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="cpu" class="com.hqd.test.ch02.bean.CPU"/>
<bean id="gpu" class="com.hqd.test.ch02.bean.GPU"/>
<bean id="disk" class="com.hqd.test.ch02.bean.Disk"/>
<bean id="memory" class="com.hqd.test.ch02.bean.Memory"/>
<bean id="computer" class="com.hqd.test.ch02.bean.Computer" autowire="byName" destroy-method="powerOff"/>
<!-- set方法注入 -->
<bean id="setComputer" class="com.hqd.test.ch02.bean.Computer" destroy-method="powerOff">
<property name="cpu" ref="cpu"/>
<property name="gpu" ref="gpu"/>
<property name="disk" ref="disk"/>
<property name="memory" ref="memory"/>
</bean>
<!-- 构造器注入 -->
<bean id="constructorComputer" class="com.hqd.test.ch02.bean.Computer" destroy-method="powerOff">
<constructor-arg ref="cpu" index="0"/>
<constructor-arg ref="gpu" index="1"/>
<constructor-arg ref="dist" index="2"/>
<constructor-arg ref="memory" index="3"/>
</bean>
</beans>
利用XML描述对象之间的关系后,我们只需叫Spring拿个电脑,开机即可,如下:
@Test
public void ioc() {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("ch02/bean.xml"));
Computer computer = beanFactory.getBean("computer", Computer.class);
computer.powerOn();
}
是不是简单方便了许多,而且我们再也不用关心 Computer 的内部结构了
根据上面的例子,我们似乎也能猜到一些,Spring帮我们从复杂的对象依赖关系中解脱了出来,简化了应用的开发
总结
上边主要对比了传统方式和IOC方式创建依赖对象的区别,关于IOC这部分属于个人的理解,如果有啥错误的地方欢迎的大家指出(✪ω✪)