在您的應(yīng)用程序中,由
Spring IoC
容器管理的形成其核心的對(duì)象被稱為"bean"
。一個(gè)bean
是由Spring IoC
容器實(shí)例化、組裝和管理的對(duì)象
這些 bean
是通過您提供給容器的配置元數(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 屬性來指定 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ū)傩月暶鳛?prototype
。類似地,如果您希望 Spring
每次需要時(shí)返回相同的 bean
實(shí)例,您應(yīng)該將 bean
的作用域?qū)傩月暶鳛?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
庫 - 在
com.tutorialspoint
包下創(chuàng)建Java
類HelloWorld
和MainApp
- 在
src
文件夾下創(chuàng)建Beans
配置文件Beans.xml
- 最后一步是創(chuàng)建所有
Java
文件和Bean
配置文件的內(nèi)容,并按以下說明運(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ì)于無狀態(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
庫 - 在
com.tutorialspoint
包下創(chuàng)建Java
類HelloWorld
和MainApp
- 在
src
文件夾下創(chuàng)建Beans
配置文件Beans.xml
- 最后一步是創(chuàng)建所有
Java
文件和Bean
配置文件的內(nèi)容,并按以下說明運(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
最后
為了方便其他設(shè)備和平臺(tái)的小伙伴觀看往期文章:
微信公眾號(hào)搜索:Let us Coding
,關(guān)注后即可獲取最新文章推送文章來源:http://www.zghlxwxcb.cn/news/detail-704913.html
看完如果覺得有幫助,歡迎 點(diǎn)贊、收藏、關(guān)注文章來源地址http://www.zghlxwxcb.cn/news/detail-704913.html
到了這里,關(guān)于Spring 中 Bean 的生命周期的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!