Spring - Bean 作用域

在定义 <bean> 时,您可以选择为该 bean 声明一个作用域。 例如,要强制 Spring 在每次需要时生成一个新的 bean 实例,您应该将 bean 的 scope 属性声明为 prototype。 同样,如果您希望 Spring 在每次需要时返回相同的 bean 实例,您应该将 bean 的 scope 属性声明为 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 定义所定义的对象的一个实例。 此单个实例存储在此类单例 bean 的缓存中,并且该命名 bean 的所有后续请求和引用都返回缓存的对象。

The default scope is always singleton. However, when you need one and only one instance of a bean, you can set the scope property to singleton in the bean configuration file, as shown in the following code snippet −

<!-- 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 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。
3 com.tutorialspoint 包下创建 Java 类 HelloWorldMainApp
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();
   }
}

以下是单例作用域所需的配置文件 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,则每次对特定 bean 发出请求时,Spring IoC 容器都会创建对象的新 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 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。
3 com.tutorialspoint 包下创建 Java 类 HelloWorldMainApp
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 id = "helloWorld" class = "com.tutorialspoint.HelloWorld" scope = "prototype">
   </bean>

</beans>

完成创建源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 −

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