欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Java

當(dāng)前位置:主頁 > 軟件編程 > Java >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊: 次

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

1.singleton

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

換言之,當(dāng)把一個(gè)bean定義設(shè)置為singleton作用域時(shí),Spring IOC容器只會創(chuàng)建該bean定義的唯一實(shí)例。這個(gè)單一實(shí)例會被存儲到單例緩存(singleton cache)中,并且所有針對該bean的后續(xù)請求和引用都將返回被緩存的對象實(shí)例,這里要注意的是singleton作用域和GOF設(shè)計(jì)模式中的單例是完全不同的,單例設(shè)計(jì)模式表示一個(gè)ClassLoader中只有一個(gè)class存在,而這里的singleton則表示一個(gè)容器對應(yīng)一個(gè)bean,也就是說當(dāng)一個(gè)bean被標(biāo)識為singleton時(shí)候,spring的IOC容器中只會存在一個(gè)該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來查看時(shí)間
      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,每一次請求(將其注入到另一個(gè)bean中,或者以程序的方式調(diào)用容器的getBean()方法)都會產(chǎn)生一個(gè)新的bean實(shí)例,相當(dāng)與一個(gè)new的操作,對于prototype作用域的bean,有一點(diǎn)非常重要,那就是Spring不能對一個(gè)prototype bean的整個(gè)生命周期負(fù)責(zé),容器在初始化、配置、裝飾或者是裝配完一個(gè)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)建一個(gè)新對象,所以每次的對象都不一樣。

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

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

上一篇:SpringBoot項(xiàng)目的測試類實(shí)例解析

欄    目:Java

下一篇:java中的String定義的字面量最大長度是多少

本文標(biāo)題:簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別

本文地址:http://mengdiqiu.com.cn/a1/Java/8876.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有