javascript
Spring5源码 - 01 BeanDefination源码分析
文章目錄
- 引入
- Bean實例化的原理
- singleton vs prototype
- Singleton VS Prototype 小結(jié)
- 下文
引入
Spring 是如何生成一個Bean的?
我們先看個例子
我們有個Configuration類AppConfig ,通過ComponentScan定義了掃描com.artisan目錄下所有表了標(biāo)注了注解的Bean
package com.artisan;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration @ComponentScan("com.artisan") public class AppConfig { }同時com.artisan目錄下還有A.class 和 B.class , 其中B類上標(biāo)注了@Component注解,A上僅僅一個普通的Java類
package com.artisan.test;import org.springframework.stereotype.Component;@Component public class B { } package com.artisan.test;public class A { }我們啟動下Spring容器,然后嘗試去重bean容器中獲取A和B的單例對象,看看會發(fā)生什么?
package com.artisan.test;import com.artisan.AppConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class BeanLoadTest {public static void main(String[] args) {// spring容器AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);System.out.println(ac.getBean(B.class));System.out.println(ac.getBean(A.class));} }結(jié)果顯而易見, 被標(biāo)注了@Component注解的B類,成功的從bean容器中獲取到了,而A類是無法從bean容器中獲取到的 ( No qualifying bean of type ‘com.artisan.test.A’ available)。
com.artisan.test.B@28864e92 Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.artisan.test.A' availableat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1177)at com.artisan.test.BeanLoadTest.main(BeanLoadTest.java:12)這是為什么呢?Spring的源碼是如何實現(xiàn)的呢? 接下來的文章,我們逐層揭開Sping Bean的神秘面紗 …
Bean實例化的原理
我們知道普通的Java類的實例化的過程,被JVM編譯成字節(jié)碼文件以后,通過new 關(guān)鍵字實例化。
Spring bean的實例化區(qū)別于普通Java類的實例化過程,是一個相對復(fù)雜的過程。
原理如下
1. 掃描到可以受到Spring管理的Bean,將其轉(zhuǎn)化成BeanDefinition
2. BeanDefinition是個接口,其中有很多屬性可以設(shè)置,比如 scope 、lazyInit、dependson 、autoType 、關(guān)聯(lián)的是哪個類setBeanClass 等等
3. 將BeanDefinition放入到一個Map中
4. 遍歷Map ,取出BeanDefinition,根據(jù)你上一步設(shè)置的各種屬性,去做不同的操作,比如autoType 、是否懶加載等等等等,實例化Bean
5. 實例化完成以后,將實例化好的Bean放到map中 (就是spring容器),即Spring的單例池 singletonObjectsMap。
這里只會創(chuàng)建單例模式的Bean,prototype等類型的bean不會添加到單例池中,因為prototype每次都會new一個,沒有必要去緩存。
當(dāng)然了,Spring的實現(xiàn)是很復(fù)雜的,我們這里先對其大致的過程和原理有個初步的了解,方便后續(xù)源碼的展開
singleton vs prototype
spring容器在啟動的時候就已經(jīng)將singleton的bean 緩存到 單例池中,而 prototype類型的bean ,在spring容器啟動的時候并不會被實例化,僅在調(diào)用的時候生成,且每次都一個新的對象 。
看個演示
加個斷點,
可以發(fā)現(xiàn) 還沒執(zhí)行到ac.getBean(B.class),僅在容器啟動階段就已經(jīng)實例化完成了singleton作用域的bean。
接下來我們看下prototype類型的bean
可以看到 在spring容器啟動的時候,并沒有實例化prototype類型的bean 。
Singleton VS Prototype 小結(jié)
我們看到了 Singleton VS Prototype的區(qū)別
下文
下文我們探討對象和bean的關(guān)系
總結(jié)
以上是生活随笔為你收集整理的Spring5源码 - 01 BeanDefination源码分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [并发编程] - Executor框架#
- 下一篇: Spring5源码 - 03 普通对象