溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別

發(fā)布時間:2020-08-26 20:12:12 來源:腳本之家 閱讀:152 作者:何其有靜 欄目:編程語言

這篇文章主要介紹了簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1.singleton

當(dāng)一個bean的作用域設(shè)置為singleton, 那么Spring IOC容器中只會存在一個共享的bean實(shí)例,并且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一實(shí)例。

換言之,當(dāng)把一個bean定義設(shè)置為singleton作用域時,Spring IOC容器只會創(chuàng)建該bean定義的唯一實(shí)例。這個單一實(shí)例會被存儲到單例緩存(singleton cache)中,并且所有針對該bean的后續(xù)請求和引用都將返回被緩存的對象實(shí)例,這里要注意的是singleton作用域和GOF設(shè)計(jì)模式中的單例是完全不同的,單例設(shè)計(jì)模式表示一個ClassLoader中只有一個class存在,而這里的singleton則表示一個容器對應(yīng)一個bean,也就是說當(dāng)一個bean被標(biāo)識為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){
    //獲取應(yīng)用程序上下文接口
    ApplicationContext apl = new ClassPathXmlApplicationContext("applicationContextER.xml");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      //反復(fù)調(diào)用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) {

    }

  }
}

測試結(jié)果:

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

從上面的結(jié)果可以看出,創(chuàng)建好對象之后,存入了緩存中。后面每次都是獲取的對象都是從緩存中獲取的,而不是新創(chuàng)建的。所以每次獲取的對象都是一樣的。

2.prototype

prototype作用域部署的bean,每一次請求(將其注入到另一個bean中,或者以程序的方式調(diào)用容器的getBean()方法)都會產(chǎn)生一個新的bean實(shí)例,相當(dāng)與一個new的操作,對于prototype作用域的bean,有一點(diǎn)非常重要,那就是Spring不能對一個prototype bean的整個生命周期負(fù)責(zé),容器在初始化、配置、裝飾或者是裝配完一個prototype實(shí)例后,將它交給客戶端,隨后就對該prototype實(shí)例不聞不問了。

不管何種作用域,容器都會調(diào)用所有對象的初始化生命周期回調(diào)方法,而對prototype而言,任何配置好的析構(gòu)生命周期回調(diào)方法都將不會被調(diào)用。清除prototype作用域的對象并釋放任何prototype bean所持有的昂貴資源,都是客戶端代碼的職責(zé)。(讓Spring容器釋放被singleton作用域bean占用資源的一種可行方式是,通過使用bean的后置處理器,該處理器持有要被清除的bean的引用。

applicationContextER.xml:

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

測試結(jié)果:

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

從上面的結(jié)果可以看出,每次都是創(chuàng)建一個新對象,所以每次的對象都不一樣。

總結(jié):從1和2可以看出,當(dāng)你需要全局的唯一標(biāo)示的時候可以用singleton,而且singleton只創(chuàng)建一個對象,系統(tǒng)消耗資源小.但是用singleton可能會有線程安全化的問題,這個時候就需要用到prototype ??紤]并發(fā)的問題,建議都用prototype。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI