Spring-Bean Scopes

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

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

范围和说明

singleton

这会将bean定义的范围限定为每个Spring IoC容器一个实例(默认)。

prototype

这将单个bean定义的范围限定为具有任意数量的对象实例。

request

这将bean定义的范围限定为HTTP请求。仅在可感知网络的Spring ApplicationContext上下文中有效。

session

这将bean定义的作用域限定为HTTP会话。仅在可感知网络的Spring ApplicationContext上下文中有效。

global-session

这将bean定义的作用域限定为全局HTTP会话。仅在可感知网络的Spring ApplicationContext上下文中有效。

在文,将讨论前两个范围。

singleton范围

如果将范围设置为单例,则Spring IoC容器将创建该bean定义所定义的对象的一个实例。该单个实例存储在此类单例bean的高速缓存中,并且对该命名bean的所有后续请求和引用都返回该高速缓存的对象。

默认范围始终为单例。但是,当您只需要一个bean的一个实例时,可以在bean配置文件中将scope属性设置为singleton,如以下代码片段所示-

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

例子

并执行以下步骤来创建一个Spring应用程序:

这是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,将单例作用域用于无状态的Bean。

要定义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应用程序-

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 = "prototype">
   </bean>

</beans>

完成创建源和Bean配置文件后,运行该应用程序。它将显示以下消息-

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

本文使用 文章同步助手 同步

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容