java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringIoC singleton prototype配置

Spring IoC容器Bean作用域的singleton与prototype使用配置

作者:小万哥

这篇文章主要为大家介绍了Spring IoC容器Bean作用域的singleton与prototype使用配置详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

 引言

在您的应用程序中,由 Spring IoC 容器管理的形成其核心的对象被称为 "bean"。一个 bean 是由 Spring IoC 容器实例化、组装和管理的对象

这些 bean 是通过您提供给容器的配置元数据创建的。Bean 定义包含了所谓的配置元数据,容器需要了解以下内容:

上述所有的配置元数据都转化为每个 bean 定义的以下属性集合。

序号属性和描述
1class
这是必填属性,指定要用于创建 bean 的 bean 类。
2name
此属性唯一地指定 bean 标识符。在基于 XML 的配置元数据中,您可以使用 id 和/或 name 属性来指定 bean 标识符。
3scope
此属性指定从特定 bean 定义创建的对象的范围
4constructor-arg
这用于注入依赖项
5properties
这用于注入依赖项
6autowiring mode
这用于注入依赖项
7lazy-initialization mode
延迟初始化的 bean 告诉 IoC 容器在首次请求时创建 bean 实例,而不是在启动时创建。
8initialization method
在容器设置了 bean 的所有必需属性之后,要调用的回调函数
9destruction method
在包含 bean 的容器销毁时要使用的回调函数

Spring 配置元数据

Spring IoC 容器与实际编写配置元数据的格式完全解耦。以下是向 Spring 容器提供配置元数据的三种重要方法:

您已经看到了如何将基于 XML 的配置元数据提供给容器,但让我们看一下包含不同 bean 定义的 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` 定义 -->
   <bean id = "..." class = "...">
      <!-- 此处是该 `bean` 的协作者和配置 -->
   </bean>
   <!-- 启用延迟初始化的 `bean` 定义 -->
   <bean id = "..." class = "..." lazy-init = "true">
      <!-- 此处是该 `bean` 的协作者和配置 -->
   </bean>
   <!-- 具有初始化方法的 `bean` 定义 -->
   <bean id = "..." class = "..." init-method = "...">
      <!-- 此处是该 `bean` 的协作者和配置 -->
   </bean>
   <!-- 具有销毁方法的 `bean` 定义 -->
   <bean id = "..." class = "..." destroy-method = "...">
      <!-- 此处是该 `bean` 的协作者和配置 -->
   </bean>
   <!-- 更多的 `bean` 定义在此处 -->

Spring 中的 Bean 作用域

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

Spring 框架支持以下五种作用域,其中三种仅在使用与 Web 相关的 ApplicationContext 时才可用。

序号作用域 & 描述
1singleton
将 bean 定义的作用域限制为 Spring IoC 容器中的单个实例(默认)。
2prototype
将单个 bean 定义的作用域限制为具有任意数量的对象实例。
3request
将 bean 定义的作用域限制为 HTTP 请求。仅在具有与 Web 相关的 Spring ApplicationContext 的情况下有效。
4session
将 bean 定义的作用域限制为 HTTP 会话。仅在具有与 Web 相关的 Spring ApplicationContext 的情况下有效。
5global-session
将 bean 定义的作用域限制为全局 HTTP 会话。仅在具有与 Web 相关的 Spring ApplicationContext 的情况下有效。

当讨论与

Web 相关的 Spring ApplicationContext 时,将讨论其他三种作用域。

单例作用域(singleton)

如果将作用域设置为 singleton,Spring IoC 容器将创建一个对象的确切实例,该实例由 bean 定义定义。此单个实例存储在此类单例 bean 的缓存中,对于该命名 bean 的所有后续请求和引用都会返回缓存的对象。

默认作用域始终是 singleton。但是,当您需要一个且仅一个 bean 实例时,您可以在 bean 配置文件中将作用域属性设置为 singleton,如下所示:

<!-- 具有 `singleton` 作用域的 `bean` 定义 -->
<bean id="..." class="..." scope="singleton">
   <!-- 此处放置此 `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();
   }
}

以下是 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)

如果将作用域设置为 prototype,Spring IoC 容器将在每次请求特定 bean 时创建该对象的新 bean 实例。通常,对于所有有状态的 bean,使用 prototype 作用域,对于无状态的 bean,使用 singleton 作用域。

要定义原型作用域,您可以在 bean 配置文件中将作用域属性设置为 prototype,如下所示:

<!-- 具有 `prototype` 作用域的 `bean` 定义 -->
<bean id="..." class="..." scope="prototype">
   <!-- 此处放置此 `bean` 的协作者和配置 -->
</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();
   }
}

以下是 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 : I'm object A

以上就是Spring IoC容器Bean作用域的singleton与prototype使用配置的详细内容,更多关于SpringIoC singleton prototype配置的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文