├── README.md ├── Spring源码学习一:源码分析概述.md ├── Spring源码学习二:BeanDefinition解析.md └── pdf ├── Spring源码学习一:源码分析概述.pdf ├── Spring源码学习七:web应用自动装配Spring配置文件.pdf ├── Spring源码学习三:BeanFactory解析.pdf ├── Spring源码学习九:DispatcherServlet初始化源码分析.pdf ├── Spring源码学习二:BeanDefinition解析.pdf ├── Spring源码学习五:BeanDefinition装载.pdf ├── Spring源码学习八:常用的扩展接口详解.pdf ├── Spring源码学习六:bean初始化.pdf ├── Spring源码学习十一:SpringMVC-@RequestBody接收json数据报415.pdf ├── Spring源码学习十三:IntrospectorCleanupListener解析.pdf ├── Spring源码学习十二:@Transactional是如何工作的.pdf ├── Spring源码学习十:DispatcherServlet请求分发源码分析.pdf └── Spring源码学习四:BeanDefinition装载前奏曲.pdf /README.md: -------------------------------------------------------------------------------- 1 | # Spring-learning 2 | 深入Spring源码学习笔记。如您觉得该项目对您有用,欢迎点击右上方的Star按钮,给予支持与激励! 3 | 4 | # 目录 5 | - [深入理解Spring系列之一:开篇](https://mp.weixin.qq.com/s/Grcx_x4yX3I8TDM4j6yQcw) 6 | - [深入理解Spring系列之二:BeanDefinition解析](https://mp.weixin.qq.com/s/KhiAGQ9udmb04qQKwKOJoA) 7 | - [深入理解Spring系列之三:BeanFactory解析](https://mp.weixin.qq.com/s/uk6t-l2lT1QdvANOnNTCTg) 8 | - [深入理解Spring系列之四:BeanDefinition装载前奏曲](https://mp.weixin.qq.com/s/-JF9xUgAipKgNoBy6NB4hw) 9 | - [深入理解Spring系列之五:BeanDefinition装载](https://mp.weixin.qq.com/s/1_grvpJYe8mMIAnebMdz9Q) 10 | - [深入理解Spring系列之六:bean初始化](https://mp.weixin.qq.com/s/SmtqoELzBEdZLo8wsSvUdQ) 11 | - [深入理解Spring系列之七:web应用自动装配Spring配置](https://mp.weixin.qq.com/s/Lf4akWFmcyn9ZVGUYNi0Lw) 12 | - [深入理解Spring系列之八:常用的扩展接口](https://mp.weixin.qq.com/s/XfhZltSlTall8wKwV_7fKg) 13 | - [深入理解Spring系列之九:DispatcherServlet初始化源码分析](https://mp.weixin.qq.com/s/UF9s52CBzEDmD0bwMfFw9A) 14 | - [深入理解Spring系列之十:DispatcherServlet请求分发源码分析](https://mp.weixin.qq.com/s/-kEjAeQFBYIGb0zRpST4UQ) 15 | - [深入理解Spring系列之十一:SpringMVC-@RequestBody接收json数据报415](https://mp.weixin.qq.com/s/beRttZyxM3IBJJSXsLzh5g) 16 | - [深入理解Spring系列之十二:@Transactional是如何工作的](https://mp.weixin.qq.com/s/ZwhkUQF1Nun9pNrFI-3a6w) 17 | - [深入理解Spring系列之十三:IntrospectorCleanupListener解析](https://mp.weixin.qq.com/s/6LStVBDc4vU7q1QBZkC_5w) 18 | - [深入理解Spring系列之十四:@Autowired是如何工作的](https://mp.weixin.qq.com/s/F8jRzgeDxHAbV5l-uAY3KA) 19 | - 更新中... 20 | 21 | ## 关于我 22 | * [头条号:Java实战技术](https://www.toutiao.com/c/user/62859607968/#mid=1575311975640078) 23 | * [公众号:JavaQ](https://mp.weixin.qq.com/s/QE2PY9B4iFFV9gCabkJzcw?_blank) 24 | * 微信号:wind7rui 25 | * [知识星球](https://www.jianshu.com/p/cff8bc8a0290) 26 | -------------------------------------------------------------------------------- /Spring源码学习一:源码分析概述.md: -------------------------------------------------------------------------------- 1 | Spring经过大神们的构思、编码,日积月累而来,所以,对其代码的理解也不是一朝一夕就能快速完成的。源码学习是枯燥的,需要坚持!坚持!坚持!当然也需要技巧,第一遍学习的时候,不用关注全部细节,不重要的代码可以先忽略掉,达到理解大体的架子及流程,避免第一次就陷入某个坑里出不来。第二遍针对某个流程更深入的、有针对性的去分析学习,当然遇到某个实在过不去的坎可以标记,后面再思考,毕竟是别人设计的,有些不是那么容易理解,可以使用google,次数多了,总会有收获! 2 | 首先,看一下Spring的最基本使用方式,直接看代码, 3 | 4 | ``` 5 | public class LoginService { 6 | public void login() { 7 | System.out.println("execute LoginService"); 8 | } 9 | } 10 | public class LoginResource { 11 | private LoginService loginService; 12 | public LoginService getLoginService() { 13 | return loginService; 14 | } 15 | public void setLoginService(LoginService loginService) { 16 | this.loginService = loginService; 17 | } 18 | public void login() { 19 | loginService.login(); 20 | } 21 | } 22 | ``` 23 | 24 | applicationgContext.xml 25 | ``` 26 | 27 | 32 | 33 | 34 | 35 | 36 | 37 | ``` 38 | 39 | ``` 40 | public class TestClient { 41 | @Test 42 | public void test() { 43 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationgContext.xml"); 44 | LoginResource loginResource = (LoginResource) applicationContext.getBean("loginResource"); 45 | loginResource.login(); 46 | } 47 | } 48 | ``` 49 | 50 | 51 | 概括的描述一下Spring背后的操作,解析applicationgContext.xml,将xml中定义的bean(如loginService和loginResource)解析成Spring内部的BeanDefinition,并以beanName(如loginService)为key,BeanDefinition(如loginService相应的BeanDefinition)为value存储到DefaultListableBeanFactory中的beanDefinitionMap(其实就是一个ConcurrentHashMap)中,同时将beanName存入beanDefinitionNames(List类型)中,然后遍历beanDefinitionNames中的beanName,进行bean的实例化并填充属性,在实例化的过程中,如果有依赖没有被实例化将先实例化其依赖,然后实例化本身,实例化完成后将实例存入单例bean的缓存中,当调用getBean方法时,到单例bean的缓存中查找,如果找到并经过转换后返回这个实例(如LoginResource的实例),之后就可以直接使用了。 52 | -------------------------------------------------------------------------------- /Spring源码学习二:BeanDefinition解析.md: -------------------------------------------------------------------------------- 1 | 《Spring源码学习一:源码分析概述》中写到在Spring容器启动的过程中,会将Bean解析成Spring内部的BeanDefinition结构,本篇将分析这个BeanDefinition的内部结构。 2 | 3 | 首先,看BeanDefinition源码: 4 | 5 | ``` 6 | public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { 7 |     /** 8 |      * Scope identifier for the standard singleton scope: "singleton". 9 |      *

Note that extended bean factories might support further scopes. 10 |      */ 11 |     String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; 12 |     /** 13 |      * Scope identifier for the standard prototype scope: "prototype". 14 |      *

Note that extended bean factories might support further scopes. 15 |      */ 16 |     String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; 17 |     /** 18 |      * Role hint indicating that a {@code BeanDefinition} is a major part 19 |      * of the application. Typically corresponds to a user-defined bean. 20 |      */ 21 |     int ROLE_APPLICATION = 0; 22 |     /** 23 |      * Role hint indicating that a {@code BeanDefinition} is a supporting 24 |      * part of some larger configuration, typically an outer 25 |      * {@link org.springframework.beans.factory.parsing.ComponentDefinition}. 26 |      * {@code SUPPORT} beans are considered important enough to be aware 27 |      * of when looking more closely at a particular 28 |      * {@link org.springframework.beans.factory.parsing.ComponentDefinition}, 29 |      * but not when looking at the overall configuration of an application. 30 |      */ 31 |     int ROLE_SUPPORT = 1; 32 |     /** 33 |      * Role hint indicating that a {@code BeanDefinition} is providing an 34 |      * entirely background role and has no relevance to the end-user. This hint is 35 |      * used when registering beans that are completely part of the internal workings 36 |      * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}. 37 |      */ 38 |     int ROLE_INFRASTRUCTURE = 2; 39 |     /** 40 |      * Return the name of the parent definition of this bean definition, if any. 41 |      */ 42 |     String getParentName(); 43 |     /** 44 |      * Set the name of the parent definition of this bean definition, if any. 45 |      */ 46 |     void setParentName(String parentName); 47 |     /** 48 |      * Return the current bean class name of this bean definition. 49 |      *

Note that this does not have to be the actual class name used at runtime, in 50 |      * case of a child definition overriding/inheriting the class name from its parent. 51 |      * Hence, do not consider this to be the definitive bean type at runtime but 52 |      * rather only use it for parsing purposes at the individual bean definition level. 53 |      */ 54 |     String getBeanClassName(); 55 |     /** 56 |      * Override the bean class name of this bean definition. 57 |      *

The class name can be modified during bean factory post-processing, 58 |      * typically replacing the original class name with a parsed variant of it. 59 |      */ 60 |     void setBeanClassName(String beanClassName); 61 |     /** 62 |      * Return the factory bean name, if any. 63 |      */ 64 |     String getFactoryBeanName(); 65 |     /** 66 |      * Specify the factory bean to use, if any. 67 |      */ 68 |     void setFactoryBeanName(String factoryBeanName); 69 |     /** 70 |      * Return a factory method, if any. 71 |      */ 72 |     String getFactoryMethodName(); 73 |     /** 74 |      * Specify a factory method, if any. This method will be invoked with 75 |      * constructor arguments, or with no arguments if none are specified. 76 |      * The method will be invoked on the specified factory bean, if any, 77 |      * or otherwise as a static method on the local bean class. 78 |      * @param factoryMethodName static factory method name, 79 |      * or {@code null} if normal constructor creation should be used 80 |      */ 81 |     void setFactoryMethodName(String factoryMethodName); 82 |     /** 83 |      * Return the name of the current target scope for this bean, 84 |      * or {@code null} if not known yet. 85 |      */ 86 |     String getScope(); 87 |     /** 88 |      * Override the target scope of this bean, specifying a new scope name. 89 |      */ 90 |     void setScope(String scope); 91 |     /** 92 |      * Return whether this bean should be lazily initialized, i.e. not 93 |      * eagerly instantiated on startup. Only applicable to a singleton bean. 94 |      */ 95 |     boolean isLazyInit(); 96 |     /** 97 |      * Set whether this bean should be lazily initialized. 98 |      *

If {@code false}, the bean will get instantiated on startup by bean 99 |      * factories that perform eager initialization of singletons. 100 |      */ 101 |     void setLazyInit(boolean lazyInit); 102 |     /** 103 |      * Return the bean names that this bean depends on. 104 |      */ 105 |     String[] getDependsOn(); 106 |     /** 107 |      * Set the names of the beans that this bean depends on being initialized. 108 |      * The bean factory will guarantee that these beans get initialized first. 109 |      */ 110 |     void setDependsOn(String... dependsOn); 111 |     /** 112 |      * Return whether this bean is a candidate for getting autowired into some other bean. 113 |      */ 114 |     boolean isAutowireCandidate(); 115 |     /** 116 |      * Set whether this bean is a candidate for getting autowired into some other bean. 117 |      */ 118 |     void setAutowireCandidate(boolean autowireCandidate); 119 |     /** 120 |      * Return whether this bean is a primary autowire candidate. 121 |      * If this value is true for exactly one bean among multiple 122 |      * matching candidates, it will serve as a tie-breaker. 123 |      */ 124 |     boolean isPrimary(); 125 |     /** 126 |      * Set whether this bean is a primary autowire candidate. 127 |      *

If this value is true for exactly one bean among multiple 128 |      * matching candidates, it will serve as a tie-breaker. 129 |      */ 130 |     void setPrimary(boolean primary); 131 |     /** 132 |      * Return the constructor argument values for this bean. 133 |      *

The returned instance can be modified during bean factory post-processing. 134 |      * @return the ConstructorArgumentValues object (never {@code null}) 135 |      */ 136 |     ConstructorArgumentValues getConstructorArgumentValues(); 137 |     /** 138 |      * Return the property values to be applied to a new instance of the bean. 139 |      *

The returned instance can be modified during bean factory post-processing. 140 |      * @return the MutablePropertyValues object (never {@code null}) 141 |      */ 142 |     MutablePropertyValues getPropertyValues(); 143 |     /** 144 |      * Return whether this a Singleton, with a single, shared instance 145 |      * returned on all calls. 146 |      */ 147 |     boolean isSingleton(); 148 |     /** 149 |      * Return whether this a Prototype, with an independent instance 150 |      * returned for each call. 151 |      */ 152 |     boolean isPrototype(); 153 |     /** 154 |      * Return whether this bean is "abstract", that is, not meant to be instantiated. 155 |      */ 156 |     boolean isAbstract(); 157 |     /** 158 |      * Get the role hint for this {@code BeanDefinition}. The role hint 159 |      * provides the frameworks as well as tools with an indication of 160 |      * the role and importance of a particular {@code BeanDefinition}. 161 |      */ 162 |     int getRole(); 163 |     /** 164 |      * Return a human-readable description of this bean definition. 165 |      */ 166 |     String getDescription(); 167 |     /** 168 |      * Return a description of the resource that this bean definition 169 |      * came from (for the purpose of showing context in case of errors). 170 |      */ 171 |     String getResourceDescription(); 172 |     /** 173 |      * Return the originating BeanDefinition, or {@code null} if none. 174 |      * Allows for retrieving the decorated bean definition, if any. 175 |      *

Note that this method returns the immediate originator. Iterate through the 176 |      * originator chain to find the original BeanDefinition as defined by the user. 177 |      */ 178 |     BeanDefinition getOriginatingBeanDefinition(); 179 | } 180 | ``` 181 | 182 | 可以看到上面的很多属性和方法都很熟悉,例如类名、scope、属性、构造函数参数列表、依赖的bean、是否是单例类、是否是懒加载等,其实就是将Bean的定义信息存储到这个BeanDefinition相应的属性中,后面对Bean的操作就直接对BeanDefinition进行,例如拿到这个BeanDefinition后,可以根据里面的类名、构造函数、构造函数参数,使用反射进行对象创建。BeanDefinition是一个接口,是一个抽象的定义,实际使用的是其实现类,如ChildBeanDefinition、RootBeanDefinition、GenericBeanDefinition等。BeanDefinition继承了AttributeAccessor,说明它具有处理属性的能力;BeanDefinition继承了BeanMetadataElement,说明它可以持有Bean元数据元素,作用是可以持有XML文件的一个bean标签对应的Object。 -------------------------------------------------------------------------------- /pdf/Spring源码学习一:源码分析概述.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习一:源码分析概述.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习七:web应用自动装配Spring配置文件.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习七:web应用自动装配Spring配置文件.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习三:BeanFactory解析.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习三:BeanFactory解析.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习九:DispatcherServlet初始化源码分析.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习九:DispatcherServlet初始化源码分析.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习二:BeanDefinition解析.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习二:BeanDefinition解析.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习五:BeanDefinition装载.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习五:BeanDefinition装载.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习八:常用的扩展接口详解.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习八:常用的扩展接口详解.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习六:bean初始化.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习六:bean初始化.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习十一:SpringMVC-@RequestBody接收json数据报415.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习十一:SpringMVC-@RequestBody接收json数据报415.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习十三:IntrospectorCleanupListener解析.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习十三:IntrospectorCleanupListener解析.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习十二:@Transactional是如何工作的.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习十二:@Transactional是如何工作的.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习十:DispatcherServlet请求分发源码分析.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习十:DispatcherServlet请求分发源码分析.pdf -------------------------------------------------------------------------------- /pdf/Spring源码学习四:BeanDefinition装载前奏曲.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wind7rui/Spring-learning/6b209d02d897794963c3c87bcb21e35bd4a23caa/pdf/Spring源码学习四:BeanDefinition装载前奏曲.pdf --------------------------------------------------------------------------------