关于后端:第31章-Spring-bean-作用域

4次阅读

共计 4322 个字符,预计需要花费 11 分钟才能阅读完成。

每日一句

I must say a word about fear. It is life’s only true opponent. Only fear can defeat life.
这里必须说说恐怖,它是生存惟一真正的对手,因为只有恐怖能力战胜生存。

概述

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

Spring 框架反对以下五个作用域,如果你应用 web-aware ApplicationContext 时,其中三个是可用的。

作用域 形容
singleton 该作用域将 bean 的定义的限度在每一个 Spring IoC 容器中的一个繁多实例 (默认)。
prototype 该作用域将繁多 bean 的定义限度在任意数量的对象实例。
request 该作用域将 bean 的定义限度为 HTTP 申请。只在 web-aware Spring ApplicationContext 的上下文中无效。
session 该作用域将 bean 的定义限度为 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中无效。
global-session 该作用域将 bean 的定义限度为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中无效。

本章将探讨前两个范畴,当咱们将探讨无关 web-aware Spring ApplicationContext 时,其余三个将被探讨。

singleton 作用域

如果作用域设置为 singleton,那么 Spring IoC 容器刚好创立一个由该 bean 定义的对象的实例。该繁多实例将存储在这种单例 bean 的高速缓存中,以及针对该 bean 的所有后续的申请和援用都返回缓存对象。

默认作用域是始终是 singleton,然而当仅仅须要 bean 的一个实例时,你能够在 bean 的配置文件中设置作用域的属性为 singleton,如下所示:

<!-- A bean definition with singleton scope -->
<bean 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 scope="singleton">
   </bean>

</beans>

一旦你创立源代码和 bean 配置文件实现后,咱们就能够运行该应用程序。如果你的应用程序所有都失常,将输入以下信息:

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

prototype 作用域

如果作用域设置为 prototype,那么每次特定的 bean 发出请求时 Spring IoC 容器就创建对象的新的 Bean 实例。一般说来,满状态的 bean 应用 prototype 作用域和没有状态的 bean 应用 singleton 作用域。

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

<!-- A bean definition with singleton scope -->
<bean 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 scope="prototype">
   </bean>

</beans>

一旦你创立源代码和 Bean 配置文件实现后,咱们就能够运行该应用程序。如果你的应用程序所有都失常,将输入以下信息:

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

美文佳句

一个人只有有幻想,生命就有了依靠;一个人只有不懈地追赶着幻想,活着才感觉意义深远,趣味无穷,也能力将生命的潜能施展到极致。

你好,我是 yltrcc,日常分享技术点滴,欢送关注我的公众号:ylcoder

正文完
 0