Spring - Bean 作用域



在定義<bean>時,您可以選擇宣告該bean的作用域。例如,要強制Spring每次需要時都生成一個新的bean例項,應將bean的作用域屬性宣告為prototype。同樣,如果您希望Spring每次需要時都返回相同的bean例項,則應將bean的作用域屬性宣告為singleton

Spring框架支援以下五個作用域,其中三個作用域僅在使用支援Web的ApplicationContext時才可用。

序號 作用域及描述
1

singleton

此作用域將bean定義的作用域限定為每個Spring IoC容器一個例項(預設值)。

2

prototype

此作用域將單個bean定義的作用域限定為任意數量的物件例項。

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容器將建立由該bean定義確定的物件的單個例項。此單個例項儲存在這樣的singleton bean的快取中,並且對該命名bean的所有後續請求和引用都將返回快取的物件。

預設作用域始終為singleton。但是,當您只需要一個bean的一個例項時,可以在bean配置檔案中將scope屬性設定為singleton,如下面的程式碼片段所示:

<!-- A bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton">
   <!-- collaborators and configuration for this bean go here -->
</bean>

示例

讓我們準備好一個可用的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:

步驟 描述
1 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個名為com.tutorialspoint的包。
2 Spring HelloWorld示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。
3 com.tutorialspoint包下建立Java類HelloWorldMainApp
4 src資料夾下建立Bean配置檔案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 id = "helloWorld" class = "com.tutorialspoint.HelloWorld" scope = "singleton">
   </bean>

</beans>

建立原始檔和bean配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:

Your Message : I'm object A
Your Message : I'm object A

prototype作用域

如果作用域設定為prototype,則Spring IoC容器每次對該特定bean發出請求時都會建立一個新的bean物件例項。通常,對於所有有狀態bean使用prototype作用域,對於無狀態bean使用singleton作用域。

要定義prototype作用域,可以在bean配置檔案中將scope屬性設定為prototype,如下面的程式碼片段所示:

<!-- A bean definition with prototype scope -->
<bean id = "..." class = "..." scope = "prototype">
   <!-- collaborators and configuration for this bean go here -->
</bean>

示例

讓我們準備好一個可用的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:

步驟 描述
1 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個名為com.tutorialspoint的包。
2 Spring HelloWorld示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。
3 com.tutorialspoint包下建立Java類HelloWorldMainApp
4 src資料夾下建立Bean配置檔案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 id = "helloWorld" class = "com.tutorialspoint.HelloWorld" scope = "prototype">
   </bean>

</beans>

建立原始檔和bean配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:

Your Message : I'm object A
Your Message : null
廣告