簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
這篇文章主要介紹了簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1.singleton
當一個bean的作用域設(shè)置為singleton, 那么Spring IOC容器中只會存在一個共享的bean實例,并且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一實例。
換言之,當把一個bean定義設(shè)置為singleton作用域時,Spring IOC容器只會創(chuàng)建該bean定義的唯一實例。這個單一實例會被存儲到單例緩存(singleton cache)中,并且所有針對該bean的后續(xù)請求和引用都將返回被緩存的對象實例,這里要注意的是singleton作用域和GOF設(shè)計模式中的單例是完全不同的,單例設(shè)計模式表示一個ClassLoader中只有一個class存在,而這里的singleton則表示一個容器對應(yīng)一個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){ //獲取應(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實例,相當與一個new的操作,對于prototype作用域的bean,有一點非常重要,那就是Spring不能對一個prototype bean的整個生命周期負責,容器在初始化、配置、裝飾或者是裝配完一個prototype實例后,將它交給客戶端,隨后就對該prototype實例不聞不問了。
不管何種作用域,容器都會調(diào)用所有對象的初始化生命周期回調(diào)方法,而對prototype而言,任何配置好的析構(gòu)生命周期回調(diào)方法都將不會被調(diào)用。清除prototype作用域的對象并釋放任何prototype bean所持有的昂貴資源,都是客戶端代碼的職責。(讓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可以看出,當你需要全局的唯一標示的時候可以用singleton,而且singleton只創(chuàng)建一個對象,系統(tǒng)消耗資源小.但是用singleton可能會有線程安全化的問題,這個時候就需要用到prototype ??紤]并發(fā)的問題,建議都用prototype。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn)
這篇文章主要為大家介紹了java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,多多加薪2022-02-02SpringBoot使用AOP與注解實現(xiàn)請求參數(shù)自動填充流程詳解
面向切面編程(aspect-oriented programming,AOP)主要實現(xiàn)的目的是針對業(yè)務(wù)處理過程中的切面進行提取,諸如日志、事務(wù)管理和安全這樣的系統(tǒng)服務(wù),從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率2023-02-02SpringBoot?SpringSecurity?詳細介紹(基于內(nèi)存的驗證)
這篇文章主要介紹了SpringBoot?SpringSecurity?介紹(基于內(nèi)存的驗證),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04解決Lombok使用@Builder無法build父類屬性的問題
這篇文章主要介紹了解決Lombok使用@Builder無法build父類屬性的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Spring boot + mybatis + orcale實現(xiàn)步驟實例代碼講解
這篇文章主要介紹了Spring boot + mybatis + orcale的實現(xiàn)步驟實例代碼講解,需要的朋友可以參考下2017-12-12