为了账号安全,请及时绑定邮箱和手机立即绑定

Spring认证 - Bean 范围教程

标签:
Java Spring

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

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


https://img1.sycdn.imooc.com//611f6f9e000176f409180696.jpg

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

单例范围

如果范围设置为单例,则 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>

示例

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


https://img1.sycdn.imooc.com//611f6f9f00019da409160371.jpg

这是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

原型范围

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

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

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

示例

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


https://img1.sycdn.imooc.com//611f6f9f00016f8b09140367.jpg

这是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


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消