前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring - Bean 范围教程

Spring - Bean 范围教程

原创
作者头像
IT胶囊
修改2021-08-20 18:15:45
5710
修改2021-08-20 18:15:45
举报
文章被收录于专栏:IT技能应用IT技能应用

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

Spring Framework 支持以下五个范围,其中三个仅在您使用 web-aware ApplicationContext 时可用。

在本章中,我们将讨论前两个范围,其余三个范围将在讨论 Web 感知 Spring ApplicationContext 时讨论。

单例范围

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

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

代码语言:javascript
复制
<!-- A bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton">
   <!-- collaborators and configuration for this bean go here -->
</bean>

示例

让我们有一个工作的 Eclipse IDE 并采取以下步骤来创建一个 Spring 应用程序 -

这是HelloWorld.java文件的内容-

代码语言:javascript
复制
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文件的内容-

代码语言:javascript
复制
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 -

代码语言:javascript
复制
<?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 配置文件的创建后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息 -

代码语言:javascript
复制
Your Message : I'm object A
Your Message : I'm object A

原型范围

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

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

代码语言:javascript
复制
<!-- 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文件的内容

代码语言:javascript
复制
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文件的内容-

代码语言:javascript
复制
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 -

代码语言:javascript
复制
<?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 配置文件的创建后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息 -

代码语言:javascript
复制
Your Message : I'm object A
Your Message : null

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 单例范围
    • 示例
    • 原型范围
      • 示例
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档