Spring Bean Scopes作用域
當定義一個Spring的<bean>,必須聲明bean 作用域的選項。例如,要強製Spring需要產生一個新的bean實例,應該聲明bean的scope屬性為prototype。如果你希望Spring 每次都返回同一個bean實例,應該聲明bean的作用域,方式類似屬性是單例。
Spring框架支持以下五個作用域,其中三個隻有當您使用Web感知的 ApplicationContext 可用。
範圍 | 描述 |
---|---|
singleton | This scopes the bean definition to a single instance per Spring IoC container (default). |
prototype | This scopes a single bean definition to have any number of object instances. |
request | This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext. |
session | This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. |
global-session | This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. |
本章將討論前兩個範圍和其餘三將討論的時候,我們將討論有關Web感知Spring的ApplicationContext。
singleton作用域:
如果範圍設置為單例,Spring IoC容器創建了一個由該bean定義的對象隻有一個實例。這個單一實例存儲在這樣的單例bean的高速緩存,以及所有後續請求和引用針對該bean返回緩存對象。
默認範圍是始終單例,但是當你需要bean的一個實例,可以設置的範圍屬性單例在bean配置文件中,如下圖所示:
<!-- A bean definition with singleton scope --> <bean id="..." class="..." scope="singleton"> <!-- collaborators and configuration for this bean go here --> </bean>
示例:
讓我們使用Eclipse IDE,然後按照下麵的步驟來創建一個Spring應用程序:
步驟 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld and MainApp under the com.yiibai package. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
這裡是HelloWorld.java 文件的內容:
package com.yiibai; 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.yiibai; 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 id="helloWorld" class="com.yiibai.HelloWorld" scope="singleton"> </bean> </beans>
一旦創建源代碼和bean配置文件來完成,運行應用程序。如果一切順利,這將打印以下信息:
Your Message : I'm object A Your Message : I'm object A
原型作用域:
如果範圍設置為原型,那麼Spring IoC容器創建對象的新的bean實例為每個特定的bean發出請求時的時間。作為一項規則,使用prototype作用域為所有狀態的bean類和singleton作用域為無狀態的bean。
要定義一個原型作用域,可以設置的範圍屬性為原型的bean配置文件中,如下圖所示:
<!-- A bean definition with singleton scope --> <bean id="..." class="..." scope="prototype"> <!-- collaborators and configuration for this bean go here --> </bean>
示例:
讓我們在地方工作的Eclipse IDE,然後按照下麵的步驟來創建一個Spring應用程序:
步驟 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld and MainApp under the com.yiibai package. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
這裡是HelloWorld.java 文件的內容:
package com.yiibai; 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.yiibai; 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(); } }
以下是必需的原型作用域的配置文件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 id="helloWorld" class="com.yiibai.HelloWorld" scope="prototype"> </bean> </beans>
創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,這將打印以下信息:
Your Message : I'm object A Your Message : null