Spring源碼之Bean實(shí)例化基本原理
創(chuàng)建Spring Bean實(shí)例化是Spring Bean生命周期的第一階段
Bean的生命周期主要有如下幾個(gè)步驟:
「詳細(xì)介紹:Spring In Action是這樣講的:」
- 實(shí)例化Bean對象,這個(gè)時(shí)候Bean的對象是非常低級(jí)的,基本不能夠被我們使用,因?yàn)檫B最基本的屬性都沒有設(shè)置,可以理解為連Autowired注解都是沒有解析的;
- 填充屬性,當(dāng)做完這一步,Bean對象基本是完整的了,可以理解為Autowired注解已經(jīng)解析完畢,依賴注入完成了;
- 如果Bean實(shí)現(xiàn)了BeanNameAware接口,則調(diào)用setBeanName方法;
- 如果Bean實(shí)現(xiàn)了BeanClassLoaderAware接口,則調(diào)用setBeanClassLoader方法;
- 如果Bean實(shí)現(xiàn)了BeanFactoryAware接口,則調(diào)用setBeanFactory方法;
- 調(diào)用BeanPostProcessor的postProcessBeforeInitialization方法;
- 如果Bean實(shí)現(xiàn)了InitializingBean接口,調(diào)用afterPropertiesSet方法;
- 如果Bean定義了init-method方法,則調(diào)用Bean的init-method方法;
- 調(diào)用BeanPostProcessor的postProcessAfterInitialization方法;當(dāng)進(jìn)行到這一步,Bean已經(jīng)被準(zhǔn)備就緒了,一直停留在應(yīng)用的上下文中,直到被銷毀;
- 如果應(yīng)用的上下文被銷毀了,如果Bean實(shí)現(xiàn)了DisposableBean接口,則調(diào)用destroy方法,如果Bean定義了destory-method聲明了銷毀方法也會(huì)被調(diào)用。
在實(shí)例化Bean之前在BeanDefinition里頭已經(jīng)有了所有需要實(shí)例化時(shí)用到的元數(shù)據(jù),接下來Spring只需要選擇合適的實(shí)例化方法以及策略即可。
「BeanDefinition」
Spring容器啟動(dòng)的時(shí)候會(huì)定位我們的配置文件,加載文件,并解析成Bean的定義文件BeanDefinition
右邊的Map里存儲(chǔ)這bean之間的依賴關(guān)系的定義BeanDefinition,比如OrderController依賴OrderService這種
實(shí)例化方法有兩大類分別是工廠方法和構(gòu)造方法實(shí)例化,后者是最常見的。其中Spring默認(rèn)的實(shí)例化方法就是無參構(gòu)造函數(shù)實(shí)例化。
如我們在xml里定義的以及用注解標(biāo)識(shí)的bean都是通過默認(rèn)實(shí)例化方法實(shí)例化的
實(shí)例化方法
「使靜態(tài)工廠方法實(shí)例化」
- public class FactoryInstance {
- public FactoryInstance() {
- System.out.println("instance by FactoryInstance");
- }
- }
- public class MyBeanFactory {
- public static FactoryInstance getInstanceStatic(){
- return new FactoryInstance();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <bean id="factoryInstance" class="spring.service.instance.MyBeanFactory"
- factory-method="getInstanceStatic"/>
- </beans>
「使用實(shí)例工廠方法實(shí)例化」
- public class MyBeanFactory {
- /**
- * 實(shí)例工廠創(chuàng)建bean實(shí)例
- *
- * @return
- */
- public FactoryInstance getInstance() {
- return new FactoryInstance();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 工廠實(shí)例 -- >
- <bean id="myBeanFactory" class="MyBeanFactory"/>
- <bean id="factoryInstance" factory-bean="myBeanFactory" factory-method="getInstance"/>
- </beans>
「使用無參構(gòu)造函數(shù)實(shí)例化(默認(rèn)的)」
- public class ConstructorInstance {
- public ConstructorInstance() {
- System.out.println("ConstructorInstance none args");
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <bean id="constructorInstance" class="spring.service.instance.ConstructorInstance"/>
- </beans>
「使用有參構(gòu)造函數(shù)實(shí)例化」
- public class ConstructorInstance {
- private String name;
- public ConstructorInstance(String name) {
- System.out.println("ConstructorInstance with args");
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <bean id="constructorInstance" class="spring.service.instance.ConstructorInstance">
- <constructor-arg index="0" name="name" value="test constructor with args"/>
- </bean>
- </beans>
源碼閱讀
直接來看看doCreateBean方法
具體實(shí)現(xiàn)在AbstractAutowireCapableBeanFactory類里面。
我們這里只需關(guān)注第一步創(chuàng)建bean實(shí)例的流程即可
- instanceWrapper = createBeanInstance(beanName, mbd, args);
上面代碼就是spring 實(shí)現(xiàn)bean實(shí)例創(chuàng)建的核心代碼。這一步主要根據(jù)BeanDefinition里的元數(shù)據(jù)定義決定使用哪種實(shí)例化方法,主要有下面三種:
- instantiateUsingFactoryMethod 工廠方法實(shí)例化的具體實(shí)現(xiàn)
- autowireConstructor 有參構(gòu)造函數(shù)實(shí)例化的具體實(shí)現(xiàn)
- instantiateBean 默認(rèn)實(shí)例化具體實(shí)現(xiàn)(無參構(gòu)造函數(shù))
「實(shí)例化策略(cglib or 反射)」
❝工廠方法的實(shí)例化手段沒有選擇策略直接用了反射實(shí)現(xiàn)的,所以這個(gè)實(shí)例化策略都是對于構(gòu)造函數(shù)實(shí)例化而言的❞
下面選一個(gè)instantiateBean的實(shí)現(xiàn)來介紹
上面說到的兩構(gòu)造函數(shù)實(shí)例化方法不管是哪一種都會(huì)選一個(gè)實(shí)例化策略進(jìn)行,到底選哪一種策略也是根據(jù)BeanDefinition里的定義決定的。
下面這一行代碼就是選擇實(shí)例化策略的代碼
- beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
「選擇使用反射還是cglib」
先判斷如果beanDefinition.getMethodOverrides()為空也就是用戶沒有使用replace或者lookup的配置方法,那么直接使用反射的方式,簡單快捷
但是如果使用了這兩個(gè)特性,在直接使用反射的方式創(chuàng)建實(shí)例就不妥了,因?yàn)樾枰獙⑦@兩個(gè)配置提供的功能切入進(jìn)去,所以就必須要使用動(dòng)態(tài)代理的方式將包含兩個(gè)特性所對應(yīng)的邏輯的攔截增強(qiáng)器設(shè)置進(jìn)去,這樣才可以保證在調(diào)用方法的時(shí)候會(huì)被相應(yīng)的攔截器增強(qiáng),返回值為包含攔截器的代理實(shí)例-----Spring源碼深度解析
- <bean id="constructorInstance" class="spring.service.instance.ConstructorInstance" >
- <lookup-method name="getName" bean="xxx"/>
- <replaced-method name="getName" replacer="yyy"/>
- </bean>
如果使用了lookup或者replaced的配置的話會(huì)使用cglib,否則直接使用反射。
- public static final String LOOKUP_METHOD_ELEMENT = "lookup-method";
- public static final String REPLACED_METHOD_ELEMENT = "replaced-method";
覺得不錯(cuò),點(diǎn)個(gè)贊再走吧,謝謝
參考:
Spring源碼深度解析
Spring In Action
https://url.ms/owy8p
本文轉(zhuǎn)載自微信公眾號(hào)「月伴飛魚」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系月伴飛魚公眾號(hào)。