1. 程式人生 > >易學筆記--第2章:spring中的Bean/2.4 延遲初始化

易學筆記--第2章:spring中的Bean/2.4 延遲初始化

第2章:spring中的Bean/2.4 延遲初始化/2.4.1  概念

  1. 針對作用域是singleton的Bean
    1. 預設情況下Bean的建立時在spring容器的啟動階段完成的,這樣的好處是:
      1. 以後需要用到Bean的時候可以隨時從容器中獲取,而不需要再次建立
      2. 壞處是初始化階段需要佔用一定的資源,特別是那些比較複雜的bean更加會耗時,結果是會拖累spring容器的啟動時間
  2. 針對作用域是prototype總是延遲初始化,也就是在需要的時候再初始化
  3. 延遲初始化
    1. 概念:正是為了解決一些特定Ben的初始化問題,讓這些bean再需要用到的時候再初始化
    2. 操作:通過引用該bean或者顯式呼叫查詢bean工作(也就是getBean操作)

第2章:spring中的Bean/2.4 延遲初始化/2.4.2  延遲操作實現/2.4.2.1 XML方式

  1. 設定全部的Bean都延遲
    1. XML檔案:設定延遲初始化標誌,true表示為延遲,false表示為不延遲

      <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"

           default-lazy-init="true">

    2. 呼叫結果:初始化操作在spring容器啟動完成後

      ==============容器載入完成之後================

      AccountDaoInMemoryImpl類初始化

      AccountServiceImpl類初始化

  2. 設定某個Bean延遲
    1. XML檔案:設定延遲初始化標誌,true表示為延遲,false表示為不延遲

      <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"

           default-lazy-init="true">

    2. 設定某個Bean不延遲

      <!-- 被依賴的bean的定義 -->

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" lazy-init="false">

           </bean>

    3. 呼叫結果:初始化操作在spring容器啟動完成後

      AccountDaoInMemoryImpl類初始化

      ==============容器載入完成之後================

      AccountServiceImpl類初始化

       


第2章:spring中的Bean/2.4 延遲初始化/2.4.2  延遲操作實現/2.4.2.2 JAVA配置方式

  1. 使用@Lazy註解,其中true表示延遲,false表示不延遲
  2. 舉例:
    1. 配置類定義:

      @Bean

           //true:延遲初始化

           @Lazy(true)

           public AccountService accountService() {

                 System.out.println("accountService方法被呼叫");

                 AccountServiceImpl bean = new AccountServiceImpl();

                 bean.setAccountDao(accountDao());

                 return bean;

           }

           

           @Bean

           //false:不延遲初始化,預設就是不延遲初始化

           @Lazy(false)

           public AccountDao accountDao() {

                 System.out.println("AccountDao方法被呼叫");

                 AccountDao bean = new AccountDaoInMemoryImpl();

                 //depedencies of accountDao bean will be injected here...

                 return bean;

           }

    2. 呼叫結果:

      AccountDao方法被呼叫

      ==============容器載入完成之後================

      accountService方法被呼叫


第2章:spring中的Bean/2.4 延遲初始化/2.4.2  延遲操作實現/2.4.2.3 註解方式

  1. 在註解類上面也是使用@Lazy註解進行標識
  2. 舉例:
    1. 註解類:
      1. 延遲

        @Service

        //延遲初始化

        @Lazy(true)

        public class AccountServiceImpl implements AccountService {

             private AccountDao accountDao;

             AccountServiceImpl(){

                System.out.println("AccountServiceImpl類初始化");

          }

      2. 不延遲

        @Repository("accountDao")

        //不延遲初始化,預設也是不延遲初始化,而且要求作用域範圍是singleton

        @Lazy(false)

        public class AccountDaoInMemoryImpl implements AccountDao {

             

             AccountDaoInMemoryImpl(){

                System.out.println("AccountDaoInMemoryImpl類初始化");

          }

    2. 呼叫結果:

      AccountDaoInMemoryImpl類初始化

      ==============容器載入完成之後================

      AccountServiceImpl類初始化


第2章:spring中的Bean/2.4 延遲初始化/2.4.3  被依賴的Bean:延遲不生效

  1. 如果某個Bean(比如A)被其它的Bean依賴,如果其它Bean在spring容器啟動時被初始化,那麼某個Bean(比如A)的延遲機制不會生效
  2. 舉例:
    1. 依賴的Bean,不設定了延遲機制

           <bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl"  lazy-init="false">

                 <!-- bean中某個屬性的定義:它的初始化值為另外的一個bean -->

                 <property name="accountDaoDao" ref="accountDao" />

           </bean>

    2. 被依賴的Bean,設定了延遲機制

      <!-- 被依賴的bean的定義 -->

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" lazy-init="true">

           </bean>

    3. 呼叫結果:被依賴的Bean也被初始化了

      AccountServiceImpl類初始化

      AccountDaoInMemoryImpl類初始化

      ==============容器載入完成之後================