1.7. Bean Definition Inheritance(Bean定义继承)

本文档详细介绍了Spring框架的核心组件,特别是IoC容器和Bean的配置。主要内容包括Bean定义的继承机制,允许子Bean从父Bean继承配置信息,如构造函数参数、属性值和生命周期回调。子Bean可以覆盖或添加新的配置,同时保留容器扩展点的灵活性。文章还强调了如何在XML配置中使用parent属性来指定Bean的继承关系,以及如何处理抽象Bean定义。
摘要由CSDN通过智能技术生成

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

1.5. Bean Scopes(Bean作用域)

1.6. Customizing the Nature of a Bean (自定义bean的性质)

1.6.1. Lifecycle Callbacks 生命周期回调

1.6.1.1. Initialization Callbacks 初始化回调

1.6.1.2. Destruction Callbacks 销毁回调

1.6.1.3. Default Initialization and Destroy Methods默认初始化和销毁方法

1.6.1.4. Combining Lifecycle Mechanisms (合并生命周期机制)

1.6.1.5. Startup and Shutdown Callbacks 启动和停止回调

1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序优雅地关闭Spr

1.6.2. ApplicationContextAware and BeanNameAware

1.6.3. Other Aware Interfaces 其它Aware接口

1.7. Bean Definition Inheritance(Bean定义继承)

1.8. Container Extension Points (容器扩展点)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.7. Bean Definition Inheritance(Bean定义继承)

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定义可以包含许多配置信息,包括构造函数参数、属性值和与容器相关的信息(constructor arguments, property values, and container-specific information),如初始化方法、静态工厂方法名等。子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, 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定义由ChildBeanDefinition 类表示。大多数用户在这个级别上不使用它们。相反,它们在类(如ClassPathXmlApplicationContext)中以声明方式配置bean定义。当使用基于XML的配置元数据时,可以通过使用parent 属性来指示子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"/>
    <!-- the age property value of 1 will be inherited from parent -->
</bean>

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类,但也可以重写它。在后一种情况下,子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定义继承父bean的作用域(scope)、构造函数参数值(constructor argument value)、属性值(property value)和方法重写(method override),并具有添加新值的选项。指定的任何作用域、初始化方法(initialization method)、销毁方法(destroy method)或static 工厂方法设置都将覆盖相应的父级设置。

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

其余设置始终取自子定义:依赖、自动装配模式、依赖检查、单例和延迟初始化。(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:

前述示例通过使用abstract 属性显式地将父bean定义标记为抽象的。如果父定义未指定类,则需要将父bean定义显式地标记为abstract ,如下例所示:

<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不能单独实例化,因为它不完整,并且还被显式标记为abstract。当定义是abstract时,它只能作为纯模板bean定义(pure template bean definition)使用,而纯模板bean定义充当子定义的父定义。尝试单独使用这样一个abstract bean,将其引用为另一个beanref属性,或者使用父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预实例化(pre-instantiate)所有单例(singletons)。因此,重要的是(至少对于单例bean而言),如果您有一个(父)bean定义,并且该定义仅用作模板,并且该定义指定了一个类,那么必须确保将abstract属性设置为true,否则应用程序上下文(application context)将实际(尝试)预实例化(pre-instantiate)抽象bean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值