Spring bean定义&Spring Bean 的作用域

这篇具有很好参考价值的文章主要介绍了Spring bean定义&Spring Bean 的作用域。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spring bean定义

目录

Spring bean定义

 文章来源地址https://www.toymoban.com/news/detail-715689.html

Spring配置元数据

Spring Bean 的作用域

 

singleton作用域:

 

原型作用域:

示例:


 

形成应用程序的骨干是由Spring IoC容器所管理的对象称为bean。bean被实例化,组装,并通过Spring IoC容器所管理的对象。这些bean由容器提供,例如,在XML的<bean/>定义,已经看到了前几章的形式配置元数据创建。

bean定义包含所需要的容器要知道以下称为配置元数据的信息:

  • 如何创建一个bean

  • Bean 生命周期的详细信息

  • Bean 依赖关系

上述所有配置元数据转换成一组的下列属性构成每个bean的定义。

属性 描述
class 此属性是强制性的,并指定bean类被用来创建bean。
name 此属性指定唯一bean标识符。在基于XML的配置元数据时,您可以使用id和/或name属性来指定bean标识符
scope 该属性指定一个特定的bean定义创建,它会在bean作用域本章要讨论的对象范围。
constructor-arg 这是用来注入的依赖关系,并在接下来的章节中进行讨论。
properties 这是用来注入的依赖关系,并在接下来的章节中进行讨论。
autowiring mode 这是用来注入的依赖关系,并在接下来的章节中进行讨论。
lazy-initialization mode 延迟初始化的bean告诉IoC容器创建bean实例时,它首先要求,而不是在启动时。
initialization method 回调只是在bean的所有必要属性后调用已设置的容器。它会在bean的生命周期章节中讨论。
destruction method 当包含该bean容器被销毁所使用的回调。它会在bean的生命周期章节中讨论。

 

Spring配置元数据

Spring IoC容器完全由在此配置元数据实际写入的格式解耦。有下列提供的配置元数据的Spring容器三个重要的方法:

  1. 基于XML的配置文件。

  2. 基于注解的配置

  3. 基于Java的配置

我们已经看到了基于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">

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

   <!-- A bean definition with lazy init set on -->
   <bean id="..." class="..." lazy-init="true">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

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

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

   <!-- more bean definitions go here -->

</beans>

 

有关基于注解的配置在一个单独的章节讨论。在一个单独的章节刻意保留它,因为希望能掌握一些Spring其他的重要概念,在开始用注解依赖注入来编程。

Spring Bean 的作用域

 

 

 

当定义一个Spring的<bean>,必须声明bean 作用域的选项。例如,要强制Spring需要产生一个新的bean实例,应该声明bean的scope属性为prototype。如果你希望Spring 每次都返回同一个bean实例,应该声明bean的作用域,方式类似属性是单例。

Spring框架支持以下五个作用域,其中三个只有当您使用Web感知的 ApplicationContext 可用。

范围 描述
singleton This scopes the bean definition to a single instance per Spring IoC container (default).
prototype This scopes a single bean definition to have any number of object instances.
request This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
session This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

本章将讨论前两个范围和其余三将讨论的时候,我们将讨论有关Web感知Spring的ApplicationContext。

 

singleton作用域:

如果范围设置为单例,Spring IoC容器创建了一个由该bean定义的对象只有一个实例。这个单一实例存储在这样的单例bean的高速缓存,以及所有后续请求和引用针对该bean返回缓存对象。

默认范围是始终单例,但是当你需要bean的一个实例,可以设置的范围属性单例在bean配置文件中,如下图所示:

 

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

示例:

让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤 描述
1 Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.manongjc package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是HelloWorld.java 文件的内容:

package com.manongjc;

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.manongjc;

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.manongjc.HelloWorld" 
      scope="singleton">
   </bean>

</beans>​

一旦创建源代码和bean配置文件来完成,运行应用程序。如果一切顺利,这将打印以下信息:

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

原型作用域:

如果范围设置为原型,那么Spring IoC容器创建对象的新的bean实例为每个特定的bean发出请求时的时间。作为一项规则,使用prototype作用域为所有状态的bean类和singleton作用域为无状态的bean。

要定义一个原型作用域,可以设置的范围属性为原型的bean配置文件中,如下图所示:

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

示例:

让我们在地方工作的Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤 描述
1 Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld and MainApp under the com.manongjc package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是HelloWorld.java 文件的内容:

​package com.manongjc;

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.manongjc;

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.manongjc.HelloWorld" 
      scope="prototype">
   </bean>

</beans>​

创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:

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

 

 

到了这里,关于Spring bean定义&Spring Bean 的作用域的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Bean作用域与生命周期

    目录 Bean的作用域: Bean有六大行为模式 1、singleton:单例模式(默认) 2、prototype: 原型模式(多例模式) 3、request: 请求作用域(Spring MVC) 4、session: 会话作用域(Spring MVC) 5、application: 全局作用域(Spring MVC) 6、websocket: HTTP WebSocket 作用域(Spring WebSocket) applicationContext和singleton的区别  Bea

    2024年02月02日
    浏览(27)
  • Spring(Bean 作用域和生命周期)

    目录 1. 案例1: Bean作用域的问题 2. 作用域 3. 设置 Bean 的作用域 4. Spring 的执行流程 5. Bean 的生命周期 现在有一个公共的 Bean,通过给 A 用户 和 B 用户使用, 然后在使用的过程中 A 偷偷的修改了公共 Bean 的数据, 导致 B 在使用时发生了预期之外的逻辑错误 (1) 公共 Bean:   [名称是

    2024年02月19日
    浏览(83)
  • Spring | Bean 作用域和生命周期

    Spring 是用来读取和存储 Bean,因此在 Spring 中 Bean 是最核心的操作资源,所以接下来我们深入学习⼀下 Bean 对象 假设现在有⼀个公共的 Bean,提供给 A 用户和 B 用户使用,然而在使用的途中 A 用户却 “悄悄” 地修改了公共 Bean 的数据,导致 B 用户在使用时发生了预期之外的逻

    2024年02月14日
    浏览(31)
  • 【Spring】Bean的作用域与生命周期详情:请简述Spring的执行流程并分析Bean的生命周期?

     我们都知道,Spring框架为开发人员提供了很多便捷,这使得开发人员能够更加专注于应用程序的核心业务逻辑,而不需要花费大量时间和精力在技术细节上。作为一个包含众多工具方法的IoC容器,存取JavaBean是其极为重要的一个环节。本文就对Spring中的Bean的作用域和生命周

    2024年02月12日
    浏览(37)
  • Bean 作用域、生命周期和Spring执行流程

    假设现在有⼀个公共的 Bean,提供给 A ⽤户和 B ⽤户使⽤,然⽽在使⽤的途中 A ⽤户却“悄悄”地修改了公共 Bean 的数据,导致 B ⽤户在使⽤时发⽣了预期之外的逻辑错误。 我们可以看到,B 用户在使用这个Bean对象时,得到的Dog是被A 用户修改过的,这无疑会给 B 用户带来很

    2024年02月12日
    浏览(32)
  • 【Spring】Bean的作用域和生命周期

    目录 一、引入案例来探讨Bean的作用域 二、Bean的作用域 2.1、Bean的6种作用域 2.2、设置Bean的作用域 三、Spring的执行流程  四、Bean的声明周期 1、生命周期演示 首先我们创建一个User类,定义一个用户信息,在定义一个Users类,使用方法注解将user存入Spring中,然后两个用户A对这

    2024年02月14日
    浏览(30)
  • Spring Bean的作用域及生命周期

    目录 前言: Bean的作用域(Scope) 单例模式 原型模式(多例作用域) 请求作用域(request) 会话作用域 全局作用域 网络长连接 Spring执行流程 Bean的生命周期 测试 小结:     使用Spring框架时,我们需要清楚Spring托管Bean的作用域和生命周期,这样使用框架才会更加得心应手。

    2024年02月03日
    浏览(23)
  • Spring Bean的作用域和生命周期

    Bean 的作用域指的是 Bean 在 Spring 容器中的行为(Bean 实例创建及生命周期),它的行为是由 Spring 来管理的,可以根据具体情况选择不同的作用域来达到性能优化、资源利用最大化和可维护性等目的。 Bean 作用域(Scope)类型主要有如下几种: 其中前两种是 Spring 核心作用域,

    2024年02月12日
    浏览(41)
  • Spring框架Bean对象的五个作用域

    ​ 在Spring项目中,那些 由Spring IoC容器所管理的对象,称为bean 。简单地讲,bean就是由Spring容器初始化、装配及管理的对象,除此之外,bean就与应用程序中的其他对象没有什么区别了。 而bean定义以及bean相互间的依赖关系将通过配置元数据来描述。 上一段描述简析: spri

    2024年03月09日
    浏览(30)
  • 5、Spring之bean的作用域和生命周期

    5.1.1.1、配置bean 注意:当bean不配置scope属性时,默认是singleton(单例) 5.1.1.2、测试 由控制台日志可知,此时ioc获取到的两个bean本质上是同一个对象 5.1.2.1、配置bean 5.1.2.2、测试 由控制台日志可知,此时ioc获取到的两个bean本质上是不同的对象 如果是在WebApplicationContext环境下

    2024年02月14日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包