Spring 简明教程
Spring - Bean Definition
构成应用程序主干且由 Spring IOC 容器管理的对象称为 beans 。Bean 是由 Spring IOC 容器实例化、组装和管理的对象。这些 Bean 是使用你提供给容器的配置元数据创建的。例如,在 XML <bean/> 定义中(你在前面章节中已经见过)。
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container. For example, in the form of XML <bean/> definitions which you have already seen in the previous chapters.
Bean 定义包含 configuration metadata 名称的信息,其中需要容器知道以下内容:
Bean definition contains the information called configuration metadata, which is needed for the container to know the following −
-
How to create a bean
-
Bean’s lifecycle details
-
Bean’s dependencies
所有以上的配置元数据都转换为一组下面的属性,该组属性构成每个 Bean 定义。
All the above configuration metadata translates into a set of the following properties that make up each bean definition.
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 Configuration Metadata
Spring IOC 容器完全解除了此配置元数据实际编写的格式上的限制。向 Spring 容器提供配置元数据的三种重要方法如下:
Spring IoC container is totally decoupled from the format in which this configuration metadata is actually written. Following are the three important methods to provide configuration metadata to the Spring Container −
-
XML based configuration file.
-
Annotation-based configuration
-
Java-based configuration
您已经了解了如何向容器提供基于 XML 的配置元数据,但我们来看另一个基于 XML 的配置文件示例,其中包含不同的 bean 定义,包括延迟初始化、初始化方法和销毁方法 −
You already have seen how XML-based configuration metadata is provided to the container, but let us see another sample of XML-based configuration file with different bean definitions including lazy initialization, initialization method, and destruction method −
<?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 Hello World Example 来了解如何定义、配置和创建 Spring Bean。
You can check Spring Hello World Example to understand how to define, configure and create Spring Beans.
我们将在单独的一章中讨论基于注解的配置。它是有意在单独的章节中讨论的,因为我们希望您掌握一些其他重要的 Spring 概念,然后再开始使用带注解的 Spring 依赖项注入进行编程。
We will discuss about Annotation Based Configuration in a separate chapter. It is intentionally discussed in a separate chapter as we want you to grasp a few other important Spring concepts, before you start programming with Spring Dependency Injection with Annotations.