Spring 5.0.0框架介绍_中英文对照_3.7

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

3.7 Bean definition inheritance

A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on. A child bean definition inherits configuration data from a parent definition. The child definition can override some values, or add others, as needed. Using parent and child bean definitions can save a lot of typing. Effectively, this is a form of templating.

Bean定义中可以包含许多配置信息,包括构造函数参数,属性值和容器的特定信息例如初始化方法,静态工厂方法名等等。子定义继承父定义的配置信息。子定义可以覆盖一些值,或按需要添加一些其它值。使用父子bean定义可以保存许多类型。实际上,这是一种模板形式。

If you work with an ApplicationContext interface programmatically, child bean definitions are represented by the ChildBeanDefinition class. Most users do not work with them on this level, instead configuring bean definitions declaratively in something like the ClassPathXmlApplicationContext. When you use XML-based configuration metadata, you indicate a child bean definition by using the parent attribute, specifying the parent bean as the value of this attribute.

如果你以编程方式使用ApplicationContext接口,那么子bean定义是通过ChildBeanDefinition定义来表示的。大多数用户不在这个层级上使用它们,而是在一些像ClassPathXmlApplicationContext中声明式的配置bean定义。当你使用基于XML的配置元数据时,你可以使用parent特性来指明一个子bean定义。在特性值中指定父bean。

1
2
3
4
5
6
7
8
9
10
11
12
<bean id="inheritedTestBean" abstract="true"
class="org.springframework.beans.TestBean">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass"
class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>

A child bean definition uses the bean class from the parent definition if none is specified, but can also override it. In the latter case, the child bean class must be compatible with the parent, that is, it must accept the parent’s property values.

如果子bean定义中没有指定要使用的bean类,则使用父定义中的bean类,但也可以覆盖它。在后一种情况下(没有指定要用的bean类),子bean定义必须与父bean协作,也就是说,它必须接收父定义的属性值。

A child bean definition inherits scope, constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any scope, initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings.

子bean定义可以继承作用域,构造函数参数值,属性值,可以重写父方法,可以选择添加新值。你指定的任何作用域,初始化方法,析构方法,和/或静态工厂方法设置将会覆盖对应的父设置。

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, lazy init.

其余的设置都是从子定义中获取:依赖关系,自动装配模式,依赖检查,单例,延迟初始化。

The preceding example explicitly marks the parent bean definition as abstract by using the abstract attribute. If the parent definition does not specify a class, explicitly marking the parent bean definition as abstract is required, as follows:

前面的例子使用抽象特性将父bean定义显式的标记为abstract。如果父定义没有指定一个类,需要显式的将父bean定义为abstract,形式如下:

1
2
3
4
5
6
7
8
9
10
<bean id="inheritedTestBeanWithoutClass" abstract="true">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBeanWithoutClass" init-method="initialize">
<property name="name" value="override"/>
<!-- age will inherit the value of 1 from the parent bean definition-->
</bean>

The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions. Trying to use such an abstract parent bean on its own, by referring to it as a ref property of another bean or doing an explicit getBean() call with the parent bean id, returns an error. Similarly, the container’s internal preInstantiateSingletons() method ignores bean definitions that are defined as abstract.

父bean不能实例化,因为它不完整,并且它被显式的标记为abstract。当一个bean定义是abstract时,它只能是一个纯粹的bean定义模板,作为一个为子定义服务的父定义。当试图使用一个abstract父bean时,可以通过另一个bean的ref属性来引用它或通过父bean的id为参数显式的调用getBean()方法,会返回一个错误。类似的,容器内部的preInstantiateSingletons()方法会忽略抽象bean定义。

ApplicationContext pre-instantiates all singletons by default. Therefore, it is important (at least for singleton beans) that if you have a (parent) bean definition which you intend to use only as a template, and this definition specifies a class, you must make sure to set the abstract attribute to true, otherwise the application context will actually (attempt to) pre-instantiate the abstract bean.

 

默认情况下ApplicationContext会预实例化所有的单例。因此,如果你想有一个(父)bean定义只作为模板来使用,这个定义中指定了一个类,那你必须确保设置abstract特性为true,否则应用上下文会(试图)预实例化这个abstract bean。

如果有收获,可以请我喝杯咖啡!