在您的应用程序中,由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配置文件的另一个示例,包括延迟初始化、初始化方法和销毁方法。- <?xml version = "1.0" encoding = "UTF-8"?>
- <beans xmlns = "http://www.springframework.org/schema/beans"
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>xsi:schemaLocation = "http://www.springframework.org/schema/beans
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id = "..." class = "...">
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean></bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id = "..." class = "..." lazy-init = "true">
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean></bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id = "..." class = "..." init-method = "...">
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean></bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id = "..." class = "..." destroy-method = "...">
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean></bean>
- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </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,如下所示:- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </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文件的内容:- package com.tutorialspoint;public class HelloWorld {<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>private String message;<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>public void setMessage(String message){<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>this.message = message;<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>}<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>public void getMessage(){<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>System.out.println("Your Message : " + message);<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>}}
复制代码 以下是MainApp.java文件的内容:- package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>public static void main(String[] args) {<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>HelloWorld objA = (HelloWorld) context.getBean("helloWorld");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>objA.setMessage("I'm object A");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>objA.getMessage();<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>HelloWorld objB = (HelloWorld) context.getBean("helloWorld");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>objB.getMessage();<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>}}
复制代码 以下是singleton作用域所需的Beans.xml配置文件的内容:- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
复制代码 当您完成创建源代码和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,如下所示:- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </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文件的内容:- package com.tutorialspoint;public class HelloWorld {<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>private String message;<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>public void setMessage(String message){<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>this.message = message;<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>}<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>public void getMessage(){<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>System.out.println("Your Message : " + message);<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>}}
复制代码 以下是MainApp.java文件的内容- package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>public static void main(String[] args) {<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>HelloWorld objA = (HelloWorld) context.getBean("helloWorld");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>objA.setMessage("I'm object A");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>objA.getMessage();<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>HelloWorld objB = (HelloWorld) context.getBean("helloWorld");<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>objB.getMessage();<bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>}}
复制代码 以下是prototype作用域所需的Beans.xml配置文件的内容:- <bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean><bean id="..." scope="singleton">
- <bean id="..." scope="prototype">
-
- </bean>
复制代码 当您完成创建源代码和bean配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:- Your Message : I'm object A
- Your Message : null
复制代码 最后
为了方便其他设备和平台的小伙伴观看往期文章,链接奉上:
公众号搜索Let us Coding,知乎,开源中国,CSDN,思否,掘金,InfoQ,简书,博客园,慕课,51CTO,helloworld,腾讯开发者社区,阿里开发者社区
看完如果觉得有帮助,欢迎点赞、收藏和关注
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |