Spring簡明分析Bean作用域
Bean作用域
經(jīng)過前面的學(xué)習(xí),我們可以知道bean是存在作用域的。
從spring的官方文檔中發(fā)現(xiàn)spring支持六種作用域,我們只需要重點(diǎn)認(rèn)識singleton、protoType即可,后面的作用域都是于web框架相關(guān)的。
一、singleton(單例模式)
就和圖中的一樣,如果bean的作用域?yàn)閟ingleton,那么在IOC容器中只有每個bean只有一個唯一的實(shí)例被創(chuàng)建。
我們通過代碼來認(rèn)識一下,bean的單例模式
bean的作用域默認(rèn)是singleton,我們也可以手動通過在xml的bean中scope進(jìn)行設(shè)置。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="pojo.User" scope="singleton"/> </beans>
根據(jù)同一個bean 獲取兩次實(shí)例,查看實(shí)例是否相同
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user1 = context.getBean("user", User.class); User user2 = context.getBean("user",User.class); System.out.println(user1==user2); }
查看運(yùn)行結(jié)果
說明 這個Bean的作用域是單例模式,根據(jù)這個bean只能創(chuàng)建一個唯一的實(shí)例。
二、protoType(原型模式)
就和圖中的一樣,如果bean的作用域?yàn)閜rotoType,那么在IOC容器中每個bean都可以創(chuàng)建多個實(shí)例。
我們通過代碼來認(rèn)識一下,bean的原型模式
bean的作用域默認(rèn)是singleton,我們也可以手動通過在xml的bean中scope進(jìn)行設(shè)置成 protoType。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="pojo.User" scope="prototype"/> </beans>
根據(jù)同一個bean 獲取兩次實(shí)例,查看實(shí)例是否相同
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user1 = context.getBean("user", User.class); User user2 = context.getBean("user",User.class); System.out.println(user1==user2); }
查看運(yùn)行結(jié)果
說明了當(dāng)設(shè)置bean為 protoType時,一個bean可以創(chuàng)建多個不同的實(shí)例。
到此這篇關(guān)于Spring簡明分析Bean作用域的文章就介紹到這了,更多相關(guān)Spring Bean作用域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot application.yml使用@@pom文件配置問題
這篇文章主要介紹了springboot application.yml使用@@pom文件配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07FactoryBean?BeanFactory方法使用示例詳解講解
這篇文章主要為大家介紹了FactoryBean?BeanFactory方法使用示例詳解講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12java中實(shí)現(xiàn)對象排序的兩種方法(Comparable,Comparator)
這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)對象排序的兩種方法,一種是實(shí)現(xiàn)Comparable進(jìn)行排序,另一種是實(shí)現(xiàn)Comparator進(jìn)行排序,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Spring JdbcTemplate整合使用方法及原理詳解
這篇文章主要介紹了Spring JdbcTemplate整合使用方法及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08springboot 中 inputStream 神秘消失之謎(終破)
這篇文章主要介紹了springboot 中 inputStream 神秘消失之謎,為了能夠把這個問題說明,我們首先需要從簡單的http調(diào)用說起,通過設(shè)置body等一些操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-08-08Java使用字節(jié)流實(shí)現(xiàn)圖片音頻的復(fù)制
今天帶大家學(xué)習(xí)Java的相關(guān)知識,文章圍繞著Java如何使用字節(jié)流實(shí)現(xiàn)圖片音頻的復(fù)制展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06Java內(nèi)存溢出的幾個區(qū)域總結(jié)(注意避坑!)
內(nèi)存溢出是指應(yīng)用系統(tǒng)中存在無法回收的內(nèi)存或使用的內(nèi)存過多,最終使得程序運(yùn)行要用到的內(nèi)存大于虛擬機(jī)能提供的最大內(nèi)存,下面這篇文章主要給大家介紹了關(guān)于Java內(nèi)存溢出的幾個區(qū)域,總結(jié)出來給大家提醒注意避坑,需要的朋友可以參考下2022-11-11