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 的依賴關系
上述所有的配置元數(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 |
| 在容器設置了 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 相關的 ApplicationContext 時才可用。
| 序號 | 作用域 & 描述 |
|---|---|
| 1 | singleton |
| 將 bean 定義的作用域限制為 Spring IoC 容器中的單個實例(默認)。 | |
| 2 | prototype |
| 將單個 bean 定義的作用域限制為具有任意數(shù)量的對象實例。 | |
| 3 | request |
| 將 bean 定義的作用域限制為 HTTP 請求。僅在具有與 Web 相關的 Spring ApplicationContext 的情況下有效。 | |
| 4 | session |
| 將 bean 定義的作用域限制為 HTTP 會話。僅在具有與 Web 相關的 Spring ApplicationContext 的情況下有效。 | |
| 5 | global-session |
| 將 bean 定義的作用域限制為全局 HTTP 會話。僅在具有與 Web 相關的 Spring ApplicationContext 的情況下有效。 |
當討論與
Web 相關的 Spring ApplicationContext 時,將討論其他三種作用域。
單例作用域(singleton)
如果將作用域設置為 singleton,Spring IoC 容器將創(chuàng)建一個對象的確切實例,該實例由 bean 定義定義。此單個實例存儲在此類單例 bean 的緩存中,對于該命名 bean 的所有后續(xù)請求和引用都會返回緩存的對象。
默認作用域始終是 singleton。但是,當您需要一個且僅一個 bean 實例時,您可以在 bean 配置文件中將作用域?qū)傩栽O置為 singleton,如下所示:
<!-- 具有 `singleton` 作用域的 `bean` 定義 --> <bean id="..." class="..." scope="singleton"> <!-- 此處放置此 `bean` 的協(xié)作者和配置 -->
示例
假設您已經(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)
如果將作用域設置為 prototype,Spring IoC 容器將在每次請求特定 bean 時創(chuàng)建該對象的新 bean 實例。通常,對于所有有狀態(tài)的 bean,使用 prototype 作用域,對于無狀態(tài)的 bean,使用 singleton 作用域。
要定義原型作用域,您可以在 bean 配置文件中將作用域?qū)傩栽O置為 prototype,如下所示:
<!-- 具有 `prototype` 作用域的 `bean` 定義 --> <bean id="..." class="..." scope="prototype"> <!-- 此處放置此 `bean` 的協(xié)作者和配置 --> </bean>
示例
假設您已經(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)容,更多關于SpringIoC singleton prototype配置的資料請關注腳本之家其它相關文章!
相關文章
一步步教你JAVA如何優(yōu)化Elastic?Search
想要榨干Java操作Elasticsearch的所有性能潛力?本指南將一步步教你如何優(yōu)化Java與Elasticsearch的交互!從此,提升ES查詢速度、降低資源消耗不再是難題,趕快一起來探索Java?Elasticsearch優(yōu)化的秘訣吧!2024-01-01
Java編寫通用的導出任何對象列表數(shù)據(jù)到excel的工具類
在工作中經(jīng)常會遇到列表數(shù)據(jù)的導出,每次需要的時候都要去開發(fā)一次,且數(shù)據(jù)不斷在變化,所以本文將利用Java編寫一個工具類可以導出任何對象列表數(shù)據(jù)到excel,希望對大家有所幫助2024-12-12
Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題
OpenCV是一個基于Apache2.0許可(開源)發(fā)行的跨平臺計算機視覺和機器學習軟件庫,可以運行在Linux、Windows、Android和Mac?OS操作系統(tǒng)上,這篇文章主要介紹了Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證,需要的朋友可以參考下2022-07-07
java實現(xiàn)DWG文件轉(zhuǎn)圖片的示例代碼
在Java中將DWG文件轉(zhuǎn)換為圖片是一個常見的需求,下面將詳細介紹如何使用Java將DWG文件轉(zhuǎn)換為圖片,并探討幾個流行的解決方案,大家可以根據(jù)需要進行選擇2025-10-10

