目录

Spring - Bean Scopes

定义时,您可以选择声明该bean的作用域。 例如,要在每次需要时强制Spring生成一个新的bean实例,您应该将bean的scope属性声明为prototype 。 类似地,如果您希望Spring在每次需要时返回相同的bean实例,则应将bean的scope属性声明为singleton

Spring Framework支持以下五个范围,其中三个范围仅在您使用支持Web的ApplicationContext时才可用。

Sr.No. 范围和描述
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,则Spring IoC容器只创建该bean定义定义的对象的一个​​实例。 此单个实例存储在此类单例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>

例子 (Example)

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3com.iowiki包下创建Java类HelloWorldMainApp
4src文件夹下创建Beans配置文件Beans.xml
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

以下是HelloWorld.java文件的内容 -

package com.iowiki;
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.iowiki;
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.iowiki.HelloWorld" scope = "singleton">
   </bean>
</beans>

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

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

原型范围

如果范围设置为prototype,则每次对该特定bean发出请求时,Spring IoC容器都会创建该对象的新bean实例。 通常,对所有state-full bean使用原型范围,对无状态bean使用singleton范围。

要定义原型范围,可以在bean配置文件中将scope属性设置为prototype ,如以下代码段所示 -

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

例子 (Example)

让我们使用Eclipse IDE并按照以下步骤创建Spring应用程序 -

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3com.iowiki包下创建Java类HelloWorldMainApp
4src文件夹下创建Beans配置文件Beans.xml
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是HelloWorld.java文件的内容

package com.iowiki;
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.iowiki;
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.iowiki.HelloWorld" scope = "prototype">
   </bean>
</beans>

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

Your Message : I'm object A
Your Message : null
↑回到顶部↑
WIKI教程 @2018