📜  Spring-Bean定义

📅  最后修改于: 2020-11-11 06:58:43             🧑  作者: Mango


构成应用程序主干并由Spring IoC容器管理的对象称为bean 。 Bean是由Spring IoC容器实例化,组装和以其他方式管理的对象。这些bean是使用您提供给容器的配置元数据创建的。例如,以XML 定义的形式出现在前面的章节中。

Bean定义包含称为配置元数据的信息,容器知道以下信息时需要使用-

  • 如何创建一个bean
  • Bean的生命周期详细信息
  • Bean的依赖

上面所有的配置元数据都转换为组成每个bean定义的一组以下属性。

Sr.No. Properties & Description
1

class

This attribute is mandatory and specifies the bean class to be used to create the bean.

2

name

This attribute specifies the bean identifier uniquely. In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s).

3

scope

This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.

4

constructor-arg

This is used to inject the dependencies and will be discussed in subsequent chapters.

5

properties

This is used to inject the dependencies and will be discussed in subsequent chapters.

6

autowiring mode

This is used to inject the dependencies and will be discussed in subsequent chapters.

7

lazy-initialization mode

A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at the startup.

8

initialization method

A callback to be called just after all necessary properties on the bean have been set by the container. It will be discussed in bean life cycle chapter.

9

destruction method

A callback to be used when the container containing the bean is destroyed. It will be discussed in bean life cycle chapter.

春季配置元数据

Spring IoC容器与实际写入此配置元数据的格式完全脱钩。以下是向Spring Container提供配置元数据的三种重要方法-

  • 基于XML的配置文件。
  • 基于注释的配置
  • 基于Java的配置

您已经了解了如何将基于XML的配置元数据提供给容器,但是让我们看一下具有不同bean定义的基于XML的配置文件的另一个示例,包括延迟初始化,初始化方法和销毁方法-





   
   
      
   

   
   
      
   

   
   
      
   

   
   
      
   

   
   

您可以查看Spring Hello World示例以了解如何定义,配置和创建Spring Bean。

我们将在单独的章节中讨论基于注释的配置。在开始使用带有注释的Spring Dependency Injection进行编程之前,我们希望在单独的章节中进行讨论,因为我们希望您掌握其他一些重要的Spring概念。