形成应用程序骨干并由 Spring IoC 容器治理的对象称为beans。bean 是由 Spring IoC 容器实例化、组装和治理的对象。这些 bean 是应用您提供给容器的配置元数据创立的。例如,您在后面的章节中曾经看到的 XML <bean/> 定义的模式。

Bean 定义蕴含称为配置元数据的信息,容器须要理解以下信息 -

如何创立一个bean
Bean 的生命周期细节
Bean 的依赖
所有上述配置元数据转换为一组以下属性,这些属性形成每个 bean 定义。

Spring 配置元数据
Spring IoC 容器与理论写入此配置元数据的格局齐全拆散。以下是为 Spring Container 提供配置元数据的三种重要办法 -

基于 XML 的配置文件。
基于注解的配置
基于Java的配置
您曾经看到了如何向容器提供基于 XML 的配置元数据,但让咱们看看另一个基于 XML 的配置文件示例,其中蕴含不同的 bean 定义,包含提早初始化、初始化办法和销毁办法 -

<?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.or...d">

<!-- 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 Hello World 示例以理解如何定义、配置和创立 Spring Bean。

咱们将在独自的章节中探讨基于注解的配置。在开始应用带有注解的 Spring 依赖注入进行编程之前,咱们无意在独自的章节中探讨它,因为咱们心愿您把握一些其余重要的 Spring 概念。