Spring?IoC容器Bean作用域的singleton與prototype使用配置
引言
在您的應(yīng)用程序中,由 Spring IoC 容器管理的形成其核心的對(duì)象被稱為 "bean"。一個(gè) bean 是由 Spring IoC 容器實(shí)例化、組裝和管理的對(duì)象
這些 bean 是通過(guò)您提供給容器的配置元數(shù)據(jù)創(chuàng)建的。Bean 定義包含了所謂的配置元數(shù)據(jù),容器需要了解以下內(nèi)容:
- 如何創(chuàng)建一個(gè) bean
- Bean 的生命周期詳細(xì)信息
- Bean 的依賴關(guān)系
上述所有的配置元數(shù)據(jù)都轉(zhuǎn)化為每個(gè) bean 定義的以下屬性集合。
序號(hào) | 屬性和描述 |
---|---|
1 | class |
這是必填屬性,指定要用于創(chuàng)建 bean 的 bean 類。 | |
2 | name |
此屬性唯一地指定 bean 標(biāo)識(shí)符。在基于 XML 的配置元數(shù)據(jù)中,您可以使用 id 和/或 name 屬性來(lái)指定 bean 標(biāo)識(shí)符。 | |
3 | scope |
此屬性指定從特定 bean 定義創(chuàng)建的對(duì)象的范圍 | |
4 | constructor-arg |
這用于注入依賴項(xiàng) | |
5 | properties |
這用于注入依賴項(xiàng) | |
6 | autowiring mode |
這用于注入依賴項(xiàng) | |
7 | lazy-initialization mode |
延遲初始化的 bean 告訴 IoC 容器在首次請(qǐng)求時(shí)創(chuàng)建 bean 實(shí)例,而不是在啟動(dòng)時(shí)創(chuàng)建。 | |
8 | initialization method |
在容器設(shè)置了 bean 的所有必需屬性之后,要調(diào)用的回調(diào)函數(shù) | |
9 | destruction method |
在包含 bean 的容器銷毀時(shí)要使用的回調(diào)函數(shù) |
Spring 配置元數(shù)據(jù)
Spring IoC 容器與實(shí)際編寫配置元數(shù)據(jù)的格式完全解耦。以下是向 Spring 容器提供配置元數(shù)據(jù)的三種重要方法:
- 基于 XML 的配置文件。
- 基于注解的配置。
- 基于 Java 的配置。
您已經(jīng)看到了如何將基于 XML 的配置元數(shù)據(jù)提供給容器,但讓我們看一下包含不同 bean 定義的 XML 配置文件的另一個(gè)示例,包括延遲初始化、初始化方法和銷毀方法。
<?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-3.0.xsd"> <!-- 一個(gè)簡(jiǎn)單的 `bean` 定義 --> <bean id = "..." class = "..."> <!-- 此處是該 `bean` 的協(xié)作者和配置 --> </bean> <!-- 啟用延遲初始化的 `bean` 定義 --> <bean id = "..." class = "..." lazy-init = "true"> <!-- 此處是該 `bean` 的協(xié)作者和配置 --> </bean> <!-- 具有初始化方法的 `bean` 定義 --> <bean id = "..." class = "..." init-method = "..."> <!-- 此處是該 `bean` 的協(xié)作者和配置 --> </bean> <!-- 具有銷毀方法的 `bean` 定義 --> <bean id = "..." class = "..." destroy-method = "..."> <!-- 此處是該 `bean` 的協(xié)作者和配置 --> </bean> <!-- 更多的 `bean` 定義在此處 -->
Spring 中的 Bean 作用域
在定義 <bean> 時(shí),您可以選擇為該 bean 聲明一個(gè)作用域。例如,要強(qiáng)制 Spring 每次需要時(shí)生成新的 bean 實(shí)例,您應(yīng)該將 bean 的作用域?qū)傩月暶鳛?nbsp;prototype。類似地,如果您希望 Spring 每次需要時(shí)返回相同的 bean 實(shí)例,您應(yīng)該將 bean 的作用域?qū)傩月暶鳛?nbsp;singleton。
Spring 框架支持以下五種作用域,其中三種僅在使用與 Web 相關(guān)的 ApplicationContext 時(shí)才可用。
序號(hào) | 作用域 & 描述 |
---|---|
1 | singleton |
將 bean 定義的作用域限制為 Spring IoC 容器中的單個(gè)實(shí)例(默認(rèn))。 | |
2 | prototype |
將單個(gè) bean 定義的作用域限制為具有任意數(shù)量的對(duì)象實(shí)例。 | |
3 | request |
將 bean 定義的作用域限制為 HTTP 請(qǐng)求。僅在具有與 Web 相關(guān)的 Spring ApplicationContext 的情況下有效。 | |
4 | session |
將 bean 定義的作用域限制為 HTTP 會(huì)話。僅在具有與 Web 相關(guān)的 Spring ApplicationContext 的情況下有效。 | |
5 | global-session |
將 bean 定義的作用域限制為全局 HTTP 會(huì)話。僅在具有與 Web 相關(guān)的 Spring ApplicationContext 的情況下有效。 |
當(dāng)討論與
Web 相關(guān)的 Spring ApplicationContext 時(shí),將討論其他三種作用域。
單例作用域(singleton)
如果將作用域設(shè)置為 singleton,Spring IoC 容器將創(chuàng)建一個(gè)對(duì)象的確切實(shí)例,該實(shí)例由 bean 定義定義。此單個(gè)實(shí)例存儲(chǔ)在此類單例 bean 的緩存中,對(duì)于該命名 bean 的所有后續(xù)請(qǐng)求和引用都會(huì)返回緩存的對(duì)象。
默認(rèn)作用域始終是 singleton。但是,當(dāng)您需要一個(gè)且僅一個(gè) bean 實(shí)例時(shí),您可以在 bean 配置文件中將作用域?qū)傩栽O(shè)置為 singleton,如下所示:
<!-- 具有 `singleton` 作用域的 `bean` 定義 --> <bean id="..." class="..." scope="singleton"> <!-- 此處放置此 `bean` 的協(xié)作者和配置 -->
示例
假設(shè)您已經(jīng)準(zhǔn)備好 Eclipse IDE,并采取以下步驟創(chuàng)建 Spring 應(yīng)用程序:
步驟
- 創(chuàng)建一個(gè)名為 SpringExample 的項(xiàng)目,在創(chuàng)建的項(xiàng)目中的 src 文件夾下創(chuàng)建一個(gè)名為 com.tutorialspoint 的包
- 使用"Add External JARs"選項(xiàng)添加所需的 Spring 庫(kù)
- 在 com.tutorialspoint 包下創(chuàng)建 Java 類 HelloWorld 和 MainApp
- 在 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml
- 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并按以下說(shuō)明運(yùn)行應(yīng)用程序。
以下是 HelloWorld.java 文件的內(nèi)容:
package com.tutorialspoint; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
以下是 MainApp.java 文件的內(nèi)容:
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.setMessage("I'm object A"); objA.getMessage(); HelloWorld objB = (HelloWorld) context.getBean("helloWorld"); objB.getMessage(); } }
以下是 singleton 作用域所需的 Beans.xml 配置文件的內(nèi)容:
<?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-3.0.xsd"> <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="singleton"> </bean> </beans>
當(dāng)您完成創(chuàng)建源代碼和 bean 配置文件后,讓我們運(yùn)行應(yīng)用程序。如果您的應(yīng)用程序一切正常,它將打印以下消息:
Your Message : I'm object A
Your Message : I'm object A
原型作用域(prototype)
如果將作用域設(shè)置為 prototype,Spring IoC 容器將在每次請(qǐng)求特定 bean 時(shí)創(chuàng)建該對(duì)象的新 bean 實(shí)例。通常,對(duì)于所有有狀態(tài)的 bean,使用 prototype 作用域,對(duì)于無(wú)狀態(tài)的 bean,使用 singleton 作用域。
要定義原型作用域,您可以在 bean 配置文件中將作用域?qū)傩栽O(shè)置為 prototype,如下所示:
<!-- 具有 `prototype` 作用域的 `bean` 定義 --> <bean id="..." class="..." scope="prototype"> <!-- 此處放置此 `bean` 的協(xié)作者和配置 --> </bean>
示例
假設(shè)您已經(jīng)準(zhǔn)備好 Eclipse IDE,并采取以下步驟創(chuàng)建 Spring 應(yīng)用程序:
步驟
- 創(chuàng)建一個(gè)名為 SpringExample 的項(xiàng)目,在創(chuàng)建的項(xiàng)目中的 src 文件夾下創(chuàng)建一個(gè)名為 com.tutorialspoint 的包
- 使用"Add External JARs"選項(xiàng)添加所需的 Spring 庫(kù)
- 在 com.tutorialspoint 包下創(chuàng)建 Java 類 HelloWorld 和 MainApp
- 在 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml
- 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并按以下說(shuō)明運(yùn)行應(yīng)用程序。
以下是 HelloWorld.java 文件的內(nèi)容:
package com.tutorialspoint; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
以下是 MainApp.java 文件的內(nèi)容:
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.setMessage("I'm object A"); objA.getMessage(); HelloWorld objB = (HelloWorld) context.getBean("helloWorld"); objB.getMessage(); } }
以下是 prototype 作用域所需的 Beans.xml 配置文件的內(nèi)容:
<?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-3.0.xsd"> <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="prototype"> </bean> </beans>
當(dāng)您完成創(chuàng)建源代碼和bean配置文件后,讓我們運(yùn)行應(yīng)用程序。如果您的應(yīng)用程序一切正常,它將打印以下消息:
Your Message : I'm object A
Your Message : I'm object A
以上就是Spring IoC容器Bean作用域的singleton與prototype使用配置的詳細(xì)內(nèi)容,更多關(guān)于SpringIoC singleton prototype配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringSecurity+JWT實(shí)現(xiàn)登錄流程分析
Spring Security 是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問(wèn)控制框架,它是為Java應(yīng)用程序設(shè)計(jì)的,特別是那些基于Spring的應(yīng)用程序,下面給大家介紹SpringSecurity+JWT實(shí)現(xiàn)登錄流程,感興趣的朋友一起看看吧2024-12-12spring mvc使用@InitBinder標(biāo)簽對(duì)表單數(shù)據(jù)綁定的方法
這篇文章主要介紹了spring mvc使用@InitBinder標(biāo)簽對(duì)表單數(shù)據(jù)綁定的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03java快速解析路徑中的參數(shù)(&與=拼接的參數(shù))
這篇文章主要介紹了java快速解析路徑中的參數(shù)(&與=拼接的參數(shù)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02深入解析Java類加載的案例與實(shí)戰(zhàn)教程
本篇文章主要介紹Tomcat類加載器架構(gòu),以及基于類加載和字節(jié)碼相關(guān)知識(shí),去分析動(dòng)態(tài)代理的原理,對(duì)Java類加載相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05Springboot通過(guò)請(qǐng)求頭獲取當(dāng)前用戶信息方法詳細(xì)示范
這篇文章主要介紹了Springboot通過(guò)請(qǐng)求頭獲取當(dāng)前用戶信息的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-11-11Mybatis框架中Interceptor接口的使用說(shuō)明
這篇文章主要介紹了Mybatis框架中Interceptor接口的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java拷貝指定目錄下所有內(nèi)容到minIO代碼實(shí)例
這篇文章主要介紹了java拷貝指定目錄下所有內(nèi)容到minIO代碼實(shí)例,創(chuàng)建桶 直接使用工具類先判斷,再創(chuàng)建即可,創(chuàng)建文件夾,需要注意以"/"結(jié)尾,實(shí)際也是在minIO上創(chuàng)建文件,只是作為目錄的表現(xiàn)形式展示,需要的朋友可以參考下2024-01-01Java處理字節(jié)類型數(shù)據(jù)的實(shí)現(xiàn)步驟
字節(jié)(Byte)是計(jì)算機(jī)信息技術(shù)用于計(jì)量存儲(chǔ)容量的一種基本單位,通常簡(jiǎn)寫為B,在ASCII編碼中1Byte可以表示一個(gè)標(biāo)準(zhǔn)的英文字符,包括大寫字母、小寫字母、數(shù)字、標(biāo)點(diǎn)符號(hào)和控制字符等,本文給大家介紹了Java如何優(yōu)雅的處理字節(jié)類型數(shù)據(jù),需要的朋友可以參考下2024-07-07