每日一句
I must say a word about fear. It is life’s only true opponent. Only fear can defeat life.
這里必須說說恐懼,它是生活惟一真正的對手,因為只有恐懼才能打敗生活,
概述
當在 Spring 中定義一個 時,你必須宣告該 bean 的作用域的選項,例如,為了強制 Spring 在每次需要時都產生一個新的 bean 實體,你應該宣告 bean 的作用域的屬性為 prototype,同理,如果你想讓 Spring 在每次需要時都回傳同一個bean實體,你應該宣告 bean 的作用域的屬性為 singleton,
Spring 框架支持以下五個作用域,如果你使用 web-aware ApplicationContext 時,其中三個是可用的,
| 作用域 | 描述 |
| singleton | 該作用域將 bean 的定義的限制在每一個 Spring IoC 容器中的一個單一實體(默認), |
| prototype | 該作用域將單一 bean 的定義限制在任意數量的物件實體, |
| request | 該作用域將 bean 的定義限制為 HTTP 請求,只在 web-aware Spring ApplicationContext 的背景關系中有效, |
| session | 該作用域將 bean 的定義限制為 HTTP 會話, 只在web-aware Spring ApplicationContext的背景關系中有效, |
| global-session | 該作用域將 bean 的定義限制為全域 HTTP 會話,只在 web-aware Spring ApplicationContext 的背景關系中有效, |
本章將討論前兩個范圍,當我們將討論有關 web-aware Spring ApplicationContext 時,其余三個將被討論,
singleton 作用域
如果作用域設定為 singleton,那么 Spring IoC 容器剛好創建一個由該 bean 定義的物件的實體,該單一實體將存盤在這種單例 bean 的高速快取中,以及針對該 bean 的所有后續的請求和參考都回傳快取物件,
默認作用域是始終是 singleton,但是當僅僅需要 bean 的一個實體時,你可以在 bean 的組態檔中設定作用域的屬性為 singleton,如下所示:
<!-- A bean definition with singleton scope -->
<bean scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>
例子
我們在適當的位置使用 Eclipse IDE,然后按照下面的步驟來創建一個 Spring 應用程式:
| 步驟 | 描述 |
| 1 | 創建一個名稱為 SpringExample 的專案,并且在創建專案的 src 檔案夾中創建一個包 com.tutorialspoint, |
| 2 | 使用 Add External JARs 選項,添加所需的 Spring 庫,在 Spring Hello World Example 章節解釋, |
| 3 | 在 com.tutorialspoint 包中創建 Java 類 HelloWorld 和 MainApp, |
| 4 | 在 src 檔案夾中創建 Beans 組態檔 Beans.xml, |
| 5 | 最后一步是創建的所有 Java 檔案和 Bean 組態檔的內容,并運行應用程式,解釋如下, |
這里是 HelloWorld.java 檔案的內容:
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 檔案的內容:
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:
<?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 scope="singleton">
</bean>
</beans>
一旦你創建源代碼和 bean 組態檔完成后,我們就可以運行該應用程式,如果你的應用程式一切都正常,將輸出以下資訊:
Your Message : I'm object A
Your Message : I'm object A
prototype 作用域
如果作用域設定為 prototype,那么每次特定的 bean 發出請求時 Spring IoC 容器就創建物件的新的 Bean 實體,一般說來,滿狀態的 bean 使用 prototype 作用域和沒有狀態的 bean 使用 singleton 作用域,
為了定義 prototype 作用域,你可以在 bean 的組態檔中設定作用域的屬性為 prototype,如下所示:
<!-- A bean definition with singleton scope -->
<bean scope="prototype">
<!-- collaborators and configuration for this bean go here -->
</bean>
例子
我們在適當的位置使用 Eclipse IDE,然后按照下面的步驟來創建一個 Spring 應用程式:
| 步驟 | 描述 |
| 1 | 創建一個名稱為 SpringExample 的專案,并且在創建專案的 src 檔案夾中創建一個包com.tutorialspoint, |
| 2 | 使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節, |
| 3 | 在 com.tutorialspoint 包中創建 Java 類 HelloWorld 和 MainApp, |
| 4 | 在 src 檔案夾中創建 Beans 組態檔Beans.xml, |
| 5 | 最后一步是創建的所有 Java 檔案和 Bean 組態檔的內容,并運行應用程式,解釋如下所示, |
這里是 HelloWorld.java 檔案的內容:
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 檔案的內容:
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:
<?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 scope="prototype">
</bean>
</beans>
一旦你創建源代碼和 Bean 組態檔完成后,我們就可以運行該應用程式,如果你的應用程式一切都正常,將輸出以下資訊:
Your Message : I'm object A
Your Message : null
美文佳句
一個人只要有夢想,生命就有了依托;一個人只有不懈地追逐著夢想,活著才覺得意義深遠,趣味無窮,也才能將生命的潛能發揮到極致,
你好,我是yltrcc,日常分享技術點滴,歡迎關注我:ylcoder
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/417074.html
標籤:其他
上一篇:一些編程實踐總結
下一篇:RabitMQ 簡介
