1. 程式人生 > >易學筆記--第2章:spring中的Bean/2.3 Bean的作用域

易學筆記--第2章:spring中的Bean/2.3 Bean的作用域

第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton

  1. 概念:指的是所建立的Bean在整個宣告週期中都是唯一的一個物件,也就是所謂的單例模式
  2. 在宣告bean的時候如果省略則預設為預設為singleton作用域

第2章:spring中的Bean/2.3 Bean的作用域/2.3.2  原型作用域:prototype

  1. 指的是每次getBean操作或者引用操作(ref bean)都會建立一個新的Bean,類似於每次都會new一個新的物件

第2章:spring中的Bean/2.3 Bean的作用域/2.3.3  請求作用域:request

 


第2章:spring中的Bean/2.3 Bean的作用域/2.3.4  會話作用域:session

 


第2章:spring中的Bean/2.3 Bean的作用域/2.3.5  作用域舉例:XML配置方式

 

  1. 單例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton
    1. 類的構造方法:

      AccountDaoInMemoryImpl(){

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

           }

    2. XML定義:

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

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" scope="singleton">

           </bean>

    3. 呼叫語句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 呼叫結果:只被初始化一次

      AccountDaoInMemoryImpl類初始化

  2. 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
    1. 類的構造方法:

      AccountDaoInMemoryImpl(){

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

           }

    2. XML定義:

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

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" scope="prototype">

           </bean>

    3. 呼叫語句:呼叫語句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 呼叫結果:會被初始化多次

      AccountDaoInMemoryImpl類初始化

      AccountDaoInMemoryImpl類初始化

      AccountDaoInMemoryImpl類初始化

  3.  

第2章:spring中的Bean/2.3 Bean的作用域/2.3.6  作用域舉例:JAVA配置方式

 

  1. 單例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton
    1. Bean方法:

           @Bean

           @Scope("singleton")

           public AccountDao accountDao() {

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

                 AccountDao bean = new AccountDaoInMemoryImpl();

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

                 return bean;

           }

    2. 呼叫語句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    3. 呼叫結果:只被初始化一次

      AccountDao方法被呼叫

  2. 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
    1. Bean方法 :

       @Bean

           @Scope("prototype")

           public AccountDao accountDao() {

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

                 AccountDao bean = new AccountDaoInMemoryImpl();

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

                 return bean;

           }

    2. 呼叫語句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    3. 呼叫結果:會被初始化多次

      AccountDao方法被呼叫

      AccountDao方法被呼叫

      AccountDao方法被呼叫


第2章:spring中的Bean/2.3 Bean的作用域/2.3.7  作用域舉例:註解方式

 

  1. 單例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton
    1. 類的構造方法:

      AccountDaoInMemoryImpl(){

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

           }

    2. 註解:

      @Repository("accountDao")

      @Scope("singleton ")

      public class AccountDaoInMemoryImpl implements AccountDao {

    3. 呼叫語句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 呼叫結果:只被初始化一次

      AccountDaoInMemoryImpl類初始化

  1. 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
    1. 類的構造方法:

      AccountDaoInMemoryImpl(){

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

           }

    2. 註解:

      @Repository("accountDao")

      @Scope("prototype ")

      public class AccountDaoInMemoryImpl implements AccountDao {

    3. 呼叫語句:呼叫語句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 呼叫結果:會被初始化多次

      AccountDaoInMemoryImpl類初始化

      AccountDaoInMemoryImpl類初始化

      AccountDaoInMemoryImpl類初始化