<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          天天用 Spring,bean 實例化原理你懂嗎?

          共 1393字,需瀏覽 3分鐘

           ·

          2020-07-27 19:18

          Java技術棧

          www.javastack.cn

          關注閱讀更多優(yōu)質文章



          來源:小小木的博客

          www.cnblogs.com/wyc1994666/p/10650480.html

          本次主要想寫spring bean的實例化相關的內容。創(chuàng)建spring bean 實例是spring bean 生命周期的第一階段。

          bean 的生命周期主要有如下幾個步驟:

          在實例化bean之前在BeanDefinition里頭已經(jīng)有了所有需要實例化時用到的元數(shù)據(jù),接下來spring 只需要選擇合適的實例化方法以及策略即可。實例化方法有兩大類分別是工廠方法和構造方法實例化,后者是最常見的。

          spring默認的實例化方法就是無參構造函數(shù)實例化。

          如我們在xml里定義的 以及用注解標識的bean都是通過默認實例化方法實例化的。

          • 兩種實例化方法(構造函數(shù) 和 工廠方法)

          • 源碼閱讀

          • 實例化策略(cglib or 反射)

          兩種實例化方

          使用適當?shù)膶嵗椒橹付ǖ腷ean創(chuàng)建新實例:工廠方法,構造函數(shù)實例化。

          代碼演示

          啟動容器時會實例化所有注冊的bean(lazy-init懶加載的bean除外),對于所有單例非懶加載的bean來說當從容器里獲取bean(getBean(String name))的時候不會觸發(fā),實例化階段,而是直接從緩存獲取已準備好的bean,而生成bean的時機則是下面這行代碼運行時觸發(fā)的。

          @Test
          public?void?testBeanInstance(){????????
          ????//?啟動容器
          ????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("spring-beans.xml");
          }

          一 使用工廠方法實例化(很少用)

          1.靜態(tài)工廠方法
          public?class?FactoryInstance?{????
          ????public?FactoryInstance()?{
          ????????System.out.println("instance?by?FactoryInstance");
          ????}
          }

          public?class?MyBeanFactory?{????public?static?FactoryInstance?getInstanceStatic(){????????return?new?FactoryInstance();
          ????}
          }
          "1.0"?encoding="UTF-8"?>"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">
          ?
          ????"factoryInstance"?class="spring.service.instance.MyBeanFactory"?factory-method="getInstanceStatic"/>


          輸出結果為:

          instance by FactoryInstance

          2.實例工廠方法
          public?class?MyBeanFactory?{????

          ????/**
          ?????*?實例工廠創(chuàng)建bean實例
          ?????*
          ?????*?@return
          ?????*/
          ????public?FactoryInstance?getInstance()?{????????return?new?FactoryInstance();
          ????}
          }
          "1.0"?encoding="UTF-8"?>
          "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">
          ???????
          ????