Spring 中 Bean 的生命周期

打印 上一主题 下一主题

主题 896|帖子 896|积分 2688

在您的应用程序中,由Spring IoC容器管理的形成其核心的对象被称为"bean"。一个bean是由Spring IoC容器实例化、组装和管理的对象。这些bean是通过您提供给容器的配置元数据创建的,例如,在前面章节中已经看到的XML  定义。
Bean定义包含了所谓的配置元数据,容器需要了解以下内容:

  • 如何创建一个bean
  • Bean的生命周期详细信息
  • Bean的依赖关系
上述所有的配置元数据都转化为每个bean定义的以下属性集合。
序号属性和描述1class这是必填属性,指定要用于创建bean的bean类。2name此属性唯一地指定bean标识符。在基于XML的配置元数据中,您可以使用id和/或name属性来指定bean标识符。3scope此属性指定从特定bean定义创建的对象的范围,将在bean范围章节中讨论。4constructor-arg这用于注入依赖项,将在后续章节中讨论。5properties这用于注入依赖项,将在后续章节中讨论。6autowiring mode这用于注入依赖项,将在后续章节中讨论。7lazy-initialization mode延迟初始化的bean告诉IoC容器在首次请求时创建bean实例,而不是在启动时创建。8initialization method在容器设置了bean的所有必需属性之后,要调用的回调函数。将在bean生命周期章节中讨论。9destruction method在包含bean的容器销毁时要使用的回调函数。将在bean生命周期章节中讨论。Spring配置元数据
Spring IoC容器与实际编写配置元数据的格式完全解耦。以下是向Spring容器提供配置元数据的三种重要方法:

  • 基于XML的配置文件。
  • 基于注解的配置。
  • 基于Java的配置。
您已经看到了如何将基于XML的配置元数据提供给容器,但让我们看一下包含不同bean定义的XML配置文件的另一个示例,包括延迟初始化、初始化方法和销毁方法。
  1. <?xml version = "1.0" encoding = "UTF-8"?>
  2. <beans xmlns = "http://www.springframework.org/schema/beans"
  3. <bean id="..."  scope="singleton">
  4. <bean id="..."  scope="prototype">
  5.    
  6. </bean>xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  7. <bean id="..."  scope="singleton">
  8. <bean id="..."  scope="prototype">
  9.    
  10. </bean>xsi:schemaLocation = "http://www.springframework.org/schema/beans
  11. <bean id="..."  scope="singleton">
  12. <bean id="..."  scope="prototype">
  13.    
  14. </bean>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  15. <bean id="..."  scope="singleton">
  16. <bean id="..."  scope="prototype">
  17.    
  18. </bean>
  19. <bean id="..."  scope="singleton">
  20. <bean id="..."  scope="prototype">
  21.    
  22. </bean><bean id = "..." class = "...">
  23. <bean id="..."  scope="singleton">
  24. <bean id="..."  scope="prototype">
  25.    
  26. </bean><bean id="..."  scope="singleton">
  27. <bean id="..."  scope="prototype">
  28.    
  29. </bean>
  30. <bean id="..."  scope="singleton">
  31. <bean id="..."  scope="prototype">
  32.    
  33. </bean></bean>
  34. <bean id="..."  scope="singleton">
  35. <bean id="..."  scope="prototype">
  36.    
  37. </bean>
  38. <bean id="..."  scope="singleton">
  39. <bean id="..."  scope="prototype">
  40.    
  41. </bean><bean id = "..." class = "..." lazy-init = "true">
  42. <bean id="..."  scope="singleton">
  43. <bean id="..."  scope="prototype">
  44.    
  45. </bean><bean id="..."  scope="singleton">
  46. <bean id="..."  scope="prototype">
  47.    
  48. </bean>
  49. <bean id="..."  scope="singleton">
  50. <bean id="..."  scope="prototype">
  51.    
  52. </bean></bean>
  53. <bean id="..."  scope="singleton">
  54. <bean id="..."  scope="prototype">
  55.    
  56. </bean>
  57. <bean id="..."  scope="singleton">
  58. <bean id="..."  scope="prototype">
  59.    
  60. </bean><bean id = "..." class = "..." init-method = "...">
  61. <bean id="..."  scope="singleton">
  62. <bean id="..."  scope="prototype">
  63.    
  64. </bean><bean id="..."  scope="singleton">
  65. <bean id="..."  scope="prototype">
  66.    
  67. </bean>
  68. <bean id="..."  scope="singleton">
  69. <bean id="..."  scope="prototype">
  70.    
  71. </bean></bean>
  72. <bean id="..."  scope="singleton">
  73. <bean id="..."  scope="prototype">
  74.    
  75. </bean>
  76. <bean id="..."  scope="singleton">
  77. <bean id="..."  scope="prototype">
  78.    
  79. </bean><bean id = "..." class = "..." destroy-method = "...">
  80. <bean id="..."  scope="singleton">
  81. <bean id="..."  scope="prototype">
  82.    
  83. </bean><bean id="..."  scope="singleton">
  84. <bean id="..."  scope="prototype">
  85.    
  86. </bean>
  87. <bean id="..."  scope="singleton">
  88. <bean id="..."  scope="prototype">
  89.    
  90. </bean></bean>
  91. <bean id="..."  scope="singleton">
  92. <bean id="..."  scope="prototype">
  93.    
  94. </bean>
复制代码
Spring中的Bean作用域
在定义时,您可以选择为该bean声明一个作用域。例如,要强制Spring每次需要时生成新的bean实例,您应该将bean的作用域属性声明为prototype。类似地,如果您希望Spring每次需要时返回相同的bean实例,您应该将bean的作用域属性声明为singleton。
Spring Framework支持以下五种作用域,其中三种仅在使用与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,如下所示:
  1. <bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean>
复制代码
示例
假设您已经准备好Eclipse IDE,并采取以下步骤创建Spring应用程序:
步骤 描述 1 创建一个名为SpringExample的项目,在创建的项目中的src文件夹下创建一个名为com.tutorialspoint的包。 2 使用"Add External JARs"选项添加所需的Spring库,如Spring Hello World示例章节中所述。 3 在com.tutorialspoint包下创建Java类HelloWorld和MainApp。 4 在src文件夹下创建Beans配置文件Beans.xml。 5 最后一步是创建所有Java文件和Bean配置文件的内容,并按以下说明运行应用程序。
以下是HelloWorld.java文件的内容:
  1. package com.tutorialspoint;public class HelloWorld {<bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean>private String message;<bean id="..."  scope="singleton">
  5. <bean id="..."  scope="prototype">
  6.    
  7. </bean>public void setMessage(String message){<bean id="..."  scope="singleton">
  8. <bean id="..."  scope="prototype">
  9.    
  10. </bean><bean id="..."  scope="singleton">
  11. <bean id="..."  scope="prototype">
  12.    
  13. </bean>this.message  = message;<bean id="..."  scope="singleton">
  14. <bean id="..."  scope="prototype">
  15.    
  16. </bean>}<bean id="..."  scope="singleton">
  17. <bean id="..."  scope="prototype">
  18.    
  19. </bean>public void getMessage(){<bean id="..."  scope="singleton">
  20. <bean id="..."  scope="prototype">
  21.    
  22. </bean><bean id="..."  scope="singleton">
  23. <bean id="..."  scope="prototype">
  24.    
  25. </bean>System.out.println("Your Message : " + message);<bean id="..."  scope="singleton">
  26. <bean id="..."  scope="prototype">
  27.    
  28. </bean>}}
复制代码
以下是MainApp.java文件的内容:
  1. package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {<bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean>public static void main(String[] args) {<bean id="..."  scope="singleton">
  5. <bean id="..."  scope="prototype">
  6.    
  7. </bean><bean id="..."  scope="singleton">
  8. <bean id="..."  scope="prototype">
  9.    
  10. </bean>ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");<bean id="..."  scope="singleton">
  11. <bean id="..."  scope="prototype">
  12.    
  13. </bean><bean id="..."  scope="singleton">
  14. <bean id="..."  scope="prototype">
  15.    
  16. </bean>HelloWorld objA = (HelloWorld) context.getBean("helloWorld");<bean id="..."  scope="singleton">
  17. <bean id="..."  scope="prototype">
  18.    
  19. </bean><bean id="..."  scope="singleton">
  20. <bean id="..."  scope="prototype">
  21.    
  22. </bean>objA.setMessage("I'm object A");<bean id="..."  scope="singleton">
  23. <bean id="..."  scope="prototype">
  24.    
  25. </bean><bean id="..."  scope="singleton">
  26. <bean id="..."  scope="prototype">
  27.    
  28. </bean>objA.getMessage();<bean id="..."  scope="singleton">
  29. <bean id="..."  scope="prototype">
  30.    
  31. </bean><bean id="..."  scope="singleton">
  32. <bean id="..."  scope="prototype">
  33.    
  34. </bean>HelloWorld objB = (HelloWorld) context.getBean("helloWorld");<bean id="..."  scope="singleton">
  35. <bean id="..."  scope="prototype">
  36.    
  37. </bean><bean id="..."  scope="singleton">
  38. <bean id="..."  scope="prototype">
  39.    
  40. </bean>objB.getMessage();<bean id="..."  scope="singleton">
  41. <bean id="..."  scope="prototype">
  42.    
  43. </bean>}}
复制代码
以下是singleton作用域所需的Beans.xml配置文件的内容:
  1. <bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean><bean id="..."  scope="singleton">
  5. <bean id="..."  scope="prototype">
  6.    
  7. </bean>
复制代码
当您完成创建源代码和bean配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:
  1. Your Message : I'm object A
  2. Your Message : I'm object A
复制代码
原型作用域(prototype)
如果将作用域设置为prototype,Spring IoC容器将在每次请求特定bean时创建该对象的新bean实例。通常,对于所有有状态的bean,使用prototype作用域,对于无状态的bean,使用singleton作用域。
要定义原型作用域,您可以在bean配置文件中将作用域属性设置为prototype,如下所示:
  1. <bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean>
复制代码
示例
假设您已经准备好Eclipse IDE,并采取以下步骤创建Spring应用程序:
步骤 描述 1 创建一个名为SpringExample的项目,在创建的项目中的src文件夹下创建一个名为com.tutorialspoint的包。 2 使用"Add External JARs"选项添加所需的Spring库,如Spring Hello World示例章节中所述。 3 在com.tutorialspoint包下创建Java类HelloWorld和MainApp。 4 在src文件夹下创建Beans配置文件Beans.xml。 5 最后一步是创建所有Java文件和Bean配置文件的内容,并按以下说明运行应用程序。
以下是HelloWorld.java文件的内容:
  1. package com.tutorialspoint;public class HelloWorld {<bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean>private String message;<bean id="..."  scope="singleton">
  5. <bean id="..."  scope="prototype">
  6.    
  7. </bean>public void setMessage(String message){<bean id="..."  scope="singleton">
  8. <bean id="..."  scope="prototype">
  9.    
  10. </bean><bean id="..."  scope="singleton">
  11. <bean id="..."  scope="prototype">
  12.    
  13. </bean>this.message  = message;<bean id="..."  scope="singleton">
  14. <bean id="..."  scope="prototype">
  15.    
  16. </bean>}<bean id="..."  scope="singleton">
  17. <bean id="..."  scope="prototype">
  18.    
  19. </bean>public void getMessage(){<bean id="..."  scope="singleton">
  20. <bean id="..."  scope="prototype">
  21.    
  22. </bean><bean id="..."  scope="singleton">
  23. <bean id="..."  scope="prototype">
  24.    
  25. </bean>System.out.println("Your Message : " + message);<bean id="..."  scope="singleton">
  26. <bean id="..."  scope="prototype">
  27.    
  28. </bean>}}
复制代码
以下是MainApp.java文件的内容
  1. package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {<bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean>public static void main(String[] args) {<bean id="..."  scope="singleton">
  5. <bean id="..."  scope="prototype">
  6.    
  7. </bean><bean id="..."  scope="singleton">
  8. <bean id="..."  scope="prototype">
  9.    
  10. </bean>ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");<bean id="..."  scope="singleton">
  11. <bean id="..."  scope="prototype">
  12.    
  13. </bean><bean id="..."  scope="singleton">
  14. <bean id="..."  scope="prototype">
  15.    
  16. </bean>HelloWorld objA = (HelloWorld) context.getBean("helloWorld");<bean id="..."  scope="singleton">
  17. <bean id="..."  scope="prototype">
  18.    
  19. </bean><bean id="..."  scope="singleton">
  20. <bean id="..."  scope="prototype">
  21.    
  22. </bean>objA.setMessage("I'm object A");<bean id="..."  scope="singleton">
  23. <bean id="..."  scope="prototype">
  24.    
  25. </bean><bean id="..."  scope="singleton">
  26. <bean id="..."  scope="prototype">
  27.    
  28. </bean>objA.getMessage();<bean id="..."  scope="singleton">
  29. <bean id="..."  scope="prototype">
  30.    
  31. </bean><bean id="..."  scope="singleton">
  32. <bean id="..."  scope="prototype">
  33.    
  34. </bean>HelloWorld objB = (HelloWorld) context.getBean("helloWorld");<bean id="..."  scope="singleton">
  35. <bean id="..."  scope="prototype">
  36.    
  37. </bean><bean id="..."  scope="singleton">
  38. <bean id="..."  scope="prototype">
  39.    
  40. </bean>objB.getMessage();<bean id="..."  scope="singleton">
  41. <bean id="..."  scope="prototype">
  42.    
  43. </bean>}}
复制代码
以下是prototype作用域所需的Beans.xml配置文件的内容:
  1. <bean id="..."  scope="singleton">
  2. <bean id="..."  scope="prototype">
  3.    
  4. </bean><bean id="..."  scope="singleton">
  5. <bean id="..."  scope="prototype">
  6.    
  7. </bean>
复制代码
当您完成创建源代码和bean配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:
  1. Your Message : I'm object A
  2. Your Message : null
复制代码
最后

为了方便其他设备和平台的小伙伴观看往期文章,链接奉上:
公众号搜索Let us Coding知乎开源中国CSDN思否掘金InfoQ简书博客园慕课51CTOhelloworld腾讯开发者社区阿里开发者社区
看完如果觉得有帮助,欢迎点赞、收藏关注

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

水军大提督

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表