1.7. Bean definition inheritance
1.7. Bean定义继承
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定义从父定义继承配置数据。 根据需要,子定义可以覆盖一些值或添加其他值。 使用父和子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的配置元数据时,可以使用父属性指定子bean定义,并指定父bean作为此属性的值。
<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"/>
</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类必须与父类兼容,也就是说,它必须接受父类的属性值。 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: 前面的示例通过使用abstract属性将父bean定义显式标记为抽象。 如果父定义没有指定类,则需要将父类定义显式标记为抽象,如下所示:
<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"/>
</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 internalpreInstantiateSingletons() method ignores bean definitions that are defined as abstract. 父bean不能自行实例化,因为它是不完整的,并且它也明确标记为抽象。 当定义像这样抽象时,它只能用作纯模板bean定义,作为子定义的父定义。 尝试单独使用这样的抽象父bean,通过将其作为另一个bean的ref属性或使用父bean id执行显式getBean()调用返回一个错误。 同样,容器的internalpreInstantiateSingletons()方法也会忽略定义为抽象的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),如果你有一个你打算只用作模板的(父)bean定义,并且这个定义指定了一个类,那么你必须确保将abstract属性设置为true ,否则应用程序上下文将实际(尝试)预先实例化抽象bean。
|