1. 程式人生 > 程式設計 >簡單瞭解spring bean作用域屬性singleton和prototype的區別

簡單瞭解spring bean作用域屬性singleton和prototype的區別

這篇文章主要介紹了簡單瞭解spring bean作用域屬性singleton和prototype的區別,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1.singleton

當一個bean的作用域設定為singleton,那麼Spring IOC容器中只會存在一個共享的bean例項,並且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一例項。

換言之,當把一個bean定義設定為singleton作用域時,Spring IOC容器只會建立該bean定義的唯一例項。這個單一例項會被儲存到單例快取(singleton cache)中,並且所有針對該bean的後續請求和引用都將返回被快取的物件例項,這裡要注意的是singleton作用域和GOF設計模式中的單例是完全不同的,單例設計模式表示一個ClassLoader中只有一個class存在,而這裡的singleton則表示一個容器對應一個bean,也就是說當一個bean被標識為singleton時候,spring的IOC容器中只會存在一個該bean。

applicationContextER.xml:

  <!--Spring bean作用域-->
  <bean id="get_date" class="java.util.Date" scope="singleton"/>

測試程式碼:

public class GetDate {
  public static void main(String[] args){
    //獲取應用程式上下文介面
    ApplicationContext apl = new ClassPathXmlApplicationContext("applicationContextER.xml");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      //反覆呼叫getBean來檢視時間
      Date date = (Date) apl.getBean("get_date");
      //休息3秒
      Thread.sleep(1000);
      System.out.println("--------------:" + simpleDateFormat.format(date));

      Date date1 = (Date) apl.getBean("get_date");
      Thread.sleep(1000);
      System.out.println("--------------:" + simpleDateFormat.format(date1));

      Date date2 = (Date) apl.getBean("get_date");
      Thread.sleep(1000);
      System.out.println("--------------:" + simpleDateFormat.format(date2));

      System.out.println("date is date1 : " + (date == date1));
      System.out.println("date1 is date2 : " + (date1 == date2));
    } catch (Exception e) {

    }

  }
}

測試結果:

23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'get_date'
23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:05:04.308 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'get_date' to allow for resolving potential circular references
23:05:04.309 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
23:05:04.310 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3108bc]
23:05:04.310 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
23:05:04.311 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
23:05:04.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date'
--------------:2019-12-21 23:05:04
23:05:05.320 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date'
--------------:2019-12-21 23:05:04
23:05:06.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date'
--------------:2019-12-21 23:05:04
date is date1 : true
date1 is date2 : true

從上面的結果可以看出,建立好物件之後,存入了快取中。後面每次都是獲取的物件都是從快取中獲取的,而不是新建立的。所以每次獲取的物件都是一樣的。

2.prototype

prototype作用域部署的bean,每一次請求(將其注入到另一個bean中,或者以程式的方式呼叫容器的getBean()方法)都會產生一個新的bean例項,相當與一個new的操作,對於prototype作用域的bean,有一點非常重要,那就是Spring不能對一個prototype bean的整個生命週期負責,容器在初始化、配置、裝飾或者是裝配完一個prototype例項後,將它交給客戶端,隨後就對該prototype例項不聞不問了。

不管何種作用域,容器都會呼叫所有物件的初始化生命週期回撥方法,而對prototype而言,任何配置好的析構生命週期回撥方法都將不會被呼叫。清除prototype作用域的物件並釋放任何prototype bean所持有的昂貴資源,都是客戶端程式碼的職責。(讓Spring容器釋放被singleton作用域bean佔用資源的一種可行方式是,通過使用bean的後置處理器,該處理器持有要被清除的bean的引用。

applicationContextER.xml:

  <!--Spring bean作用域-->
  <bean id="get_date" class="java.util.Date" scope="prototype"/>

測試結果:

23:01:51.314 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:01:51.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
--------------:2019-12-21 23:01:51
23:01:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:01:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
--------------:2019-12-21 23:01:52
23:01:53.330 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:01:53.331 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
--------------:2019-12-21 23:01:53
date is date1 : false
date1 is date2 : false

從上面的結果可以看出,每次都是建立一個新物件,所以每次的物件都不一樣。

總結:從1和2可以看出,當你需要全域性的唯一標示的時候可以用singleton,而且singleton只建立一個物件,系統消耗資源小.但是用singleton可能會有執行緒安全化的問題,這個時候就需要用到prototype 。考慮併發的問題,建議都用prototype。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。