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