Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring Bean 作用域
Spring Bean 作用域
作者:--    发布时间:2019-11-20

当在 spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项。例如,为了强制 spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean 的作用域的属性为 prototype。同理,如果你想让 spring 在每次需要时都返回同一个bean实例,你应该声明 bean 的作用域的属性为 singleton

spring 框架支持以下五个作用域,分别为singleton、prototype、request、session和global session,5种作用域说明如下所示,

注意,如果你使用 web-aware applicationcontext 时,其中三个是可用的。    

作用域 描述
singleton

在spring ioc容器仅存在一个bean实例,bean以单例方式存在,默认值

prototype 每次从容器中调用bean时,都返回一个新的实例,即每次调用getbean()时,相当于执行newxxxbean()
request 每次http请求都会创建一个新的bean,该作用域仅适用于webapplicationcontext环境
session 同一个http session共享一个bean,不同session使用不同的bean,仅适用于webapplicationcontext环境
global-session 一般用于portlet应用环境,该运用域仅适用于webapplicationcontext环境

本章将讨论前两个范围,当我们将讨论有关 web-aware spring applicationcontext 时,其余三个将被讨论。

singleton 作用域:

singleton 是默认的作用域,也就是说,当定义 bean 时,如果没有指定作用域配置项,则 bean 的作用域被默认为 singleton。

当一个bean的作用域为singleton,那么spring ioc容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。

也就是说,当将一个bean定义设置为singleton作用域的时候,spring ioc容器只会创建该bean定义的唯一实例。

singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,singleton作用域是spring中的缺省作用域。你可以在 bean 的配置文件中设置作用域的属性为 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 使用 add external jars 选项,添加所需的 spring 库,在 spring hello world example 章节解释。
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();
   }
}

下面是 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 作用域

当一个bean的作用域为prototype,表示一个bean定义对应多个对象实例。prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getbean()方法)时都会创建一个新的bean实例。prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。

为了定义 prototype 作用域,你可以在 bean 的配置文件中设置作用域的属性为 prototype,如下所示:

<!-- a bean definition with singleton 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 example 章节。
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
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册