1.7. Bean Definition Inheritance

上一节 1.6. Customizing the Nature of a Bean

目录

下一节 1.8. Container Extension Points

1.7. Bean Definition Inheritance

1.7. Bean Definition 继承

A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information, such as the initialization method, a 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 definition可以包含许多配置信息,包括构造方法参数、属性值和特定于容器的信息,
例如初始化方法、静态工厂方法名等等。
子bean definition 从父bean definition 继承配置数据。
子定义可以覆盖某些值,也可以根据需要添加其他值。
使用父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, they configure bean definitions declaratively in a class such as the ClassPathXmlApplicationContext. When you use XML-based configuration metadata, you can indicate a child bean definition by using the parent attribute, specifying the parent bean as the value of this attribute. The following example shows how to do so:

如果以编程方式使用ApplicationContext接口,则子bean definitions 由
ChildBeanDefinition类表示。
大多数用户不在这个级别上使用它们。
相反,它们在类(如ClassPathXmlApplicationContext)中声明性地配置
bean definitions。
在使用基于xml的配置元数据时,可以通过使用父属性指定父bean作为
该属性的值来指示子bean definitions。
下面的例子展示了如何做到这一点:

<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">  1 
    <property name="name" value="override"/>
    <!-- the age property value of 1 will be inherited from parent -->
</bean>
  1. Note the parent attribute.
注意这个parent属性

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 definition 使用父定义中的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, or static factory method settings that you specify override the corresponding parent settings.

子bean definition 从父bean继承作用域、构造函数参数值、属性值和方法覆盖,
并提供添加新值的选项。
您指定的任何作用域、初始化方法、销毁方法或静态工厂方法设置都会覆盖相应的
父bean的设置。

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, and 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 the following example shows:

前面的示例使用抽象属性显式地将父bean定义标记为抽象。
如果父定义没有指定类,则需要显式地将父bean definition标记为抽象,
如下面的示例所示:

<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, 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不能自己实例化,因为它不完整,而且它还显式地标记为抽象。
当定义是抽象的时候,它只能作为纯模板bean definition使用,作为子定义的父定义。
试图单独使用这样一个抽象的父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默认情况下预实例化所有单例。
因此,它是重要的(至少对 singleton bean),如果你有一个(父)bean definition
你只打算使用作为模板,这个定义指定了一个类,您必须确保设置abstract属性为true,
否则应用程序上下文会(试图)pre-instantiate抽象的bean。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值