Spring-bean-scopes

提供:Dev Guides
移動先:案内検索

春-Beanスコープ

<bean>を定義するとき、そのbeanのスコープを宣言するオプションがあります。 たとえば、Springが必要になるたびに新しいBeanインスタンスを生成するように強制するには、Beanのスコープ属性を prototype として宣言する必要があります。 同様に、Springが必要になるたびに同じBeanインスタンスを返すようにする場合は、Beanのスコープ属性を*シングルトン*として宣言する必要があります。

Spring Frameworkは、次の5つのスコープをサポートします。そのうち3つは、Web対応のApplicationContextを使用する場合にのみ使用できます。

Sr.No. Scope & Description
1

singleton

これは、Spring IoCコンテナーごとにBean定義を単一インスタンスにスコープします(デフォルト)。

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のコンテキストでのみ有効です。

この章では、最初の2つのスコープについて説明し、残りの3つのスコープについては、Web対応のSpring ApplicationContextについて説明するときに説明します。

シングルトンスコープ

スコープがシングルトンに設定されている場合、Spring IoCコンテナーは、そのBean定義で定義されたオブジェクトのインスタンスを1つだけ作成します。 この単一のインスタンスは、そのようなシングルトンBeanのキャッシュに格納され、その名前付きBeanに対する後続のすべてのリクエストと参照は、キャッシュされたオブジェクトを返します。

デフォルトのスコープは常にシングルトンです。 ただし、Beanのインスタンスが1つだけ必要な場合は、次のコードスニペットに示すように、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アプリケーションを作成します。

Steps Description
1 Create a project with a name SpringExample and create a package com.finddevguides 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.finddevguides 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.finddevguides;

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.finddevguides;

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.finddevguides.HelloWorld" scope = "singleton">
   </bean>

</beans>

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-

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

プロトタイプスコープ

スコープがプロトタイプに設定されている場合、Spring IoCコンテナーは、その特定のBeanに対する要求が行われるたびに、オブジェクトの新しいBeanインスタンスを作成します。 原則として、すべてのステートフルBeanにはプロトタイプスコープを使用し、ステートレスBeanにはシングルトンスコープを使用します。

プロトタイプスコープを定義するには、次のコードスニペットに示すように、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アプリケーションを作成します。

Steps Description
1 Create a project with a name SpringExample and create a package com.finddevguides 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.finddevguides 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.finddevguides;

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.finddevguides;

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.finddevguides.HelloWorld" scope = "prototype">
   </bean>

</beans>

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-

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