Bean定义继承

Bean定义可以包含许多配置信息,包括构造函数参数,属性值和特定于容器的信息,例如初始化方法,静态工厂方法名称等。子bean定义从父定义继承配置数据。子定义可以覆盖某些值或根据需要添加其他值。使用父bean和子bean定义可以节省很多输入。实际上,这是一种模板形式。

如果您以编程方式使用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"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>

Note the parent attribute.

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

子bean定义从父对象继承范围,构造函数参数值,属性值和方法替代,并可以选择添加新值。 您指定的任何范围,初始化方法,destroy方法或静态工厂方法设置都会覆盖相应的父设置。

其余设置始终从子定义中获取:依赖项,自动装配模式,依赖项检查,单例和惰性初始化。

前面的示例使用abstract属性将父bean定义显式标记为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"/>
<!-- age will inherit the value of 1 from the parent bean definition-->
</bean>

父bean不能单独实例化,因为它不完整,并且还被明确标记为抽象。 当定义是抽象的时,它只能用作纯模板bean定义,用作子定义的父定义。 通过将其称为另一个bean的ref属性或使用父bean ID进行显式getBean()调用来尝试单独使用这样的抽象父bean会返回错误。 同样,容器的内部preInstantiateSingletons()方法将忽略定义为抽象的bean定义。

默认情况下,ApplicationContext会预先实例化所有单例。 因此,重要的是(至少对于单例bean),如果有一个(父)bean定义仅打算用作模板,并且此定义指定了一个类,则必须确保将abstract属性设置为true ,否则应用程序上下文将实际上(试图)预先实例化抽象bean。

Spring @Configuration继承的更多相关文章

  1. Spring Configuration Check Unmapped Spring configuration files found

    Spring Configuration Check Unmapped Spring configuration files found 项目中有xml文件,但没有被用IntelliJ 导入现有工程时 ...

  2. IntelliJ 15 unmapped spring configuration files found

    IntelliJ Spring Configuration Check 用IntelliJ 导入现有工程时,如果原来的工程中有spring,每次打开工程就会提示:Spring Configuratio ...

  3. Spring @Configuration 和 @Component 区别

    Spring @Configuration 和 @Component 区别 一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个 ...

  4. 出现unmapped spring configuration files found

    intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.

  5. IntelliJ IDEA 2017 提示“Unmapped Spring configuration files found.Please configure Spring facet.”解决办法

    当把自己的一个项目导入IDEA之后,Event Log提示“Unmapped Spring configuration files found.Please configure Spring face ...

  6. "Unmapped Spring configuration files found.Please configure Spring facet."解决办法

    最近在学习使用IDEA工具,觉得与Eclipse相比,还是有很多的方便之处. 但是,当把自己的一个项目导入IDEA之后,Event Log提示"Unmapped Spring configu ...

  7. io.spring.platform继承方式和import方式更改依赖版本号的问题

    使用io.spring.platform时,它会管理各类经过集成测试的依赖版本号. 但有的时候,我们想使用指定的版本号,这个时候就需要去覆盖io.spring.platform的版本号. 前面的文章总 ...

  8. spring configuration 注解

    org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...

  9. Spring中继承配置的注入方法

    (1)两个java类.一个父类一个字类 package com.lc.inherit; /* * 这里是父类 */ public class Student { protected String na ...

随机推荐

  1. Harbor2.2.4在CentOS7.9安装、部署

    CentOS7.9基础环境配置 https://www.cnblogs.com/uncleyong/p/15471002.html 直接从网盘获取配置好的环境 修改:vim /etc/hosts 12 ...

  2. DT时代,优秀的BI工具应该具备哪些功能

    马云曾在一次演讲中说:"人类正从IT时代走向DT时代."那DT究竟是什么,和IT有什么不同呢?我们对IT非常熟悉,它是信息技术(InformationTechnology)的英文缩 ...

  3. 2021年国内BI厂商推荐_大数据分析工具

    随着互联网大数据时代的不断发展,BI让企业的工作效率变得更高效.BI的功能也随着需求的增长不断地丰富,例如,数据可视化大屏.可视化表格.商业化数据分析.数据地图等.国外的厂商在很多场景下无法满足国内的 ...

  4. 【C# .Net GC】开篇

    前言 自从.NET Core 3.0开始对根据自己具体的应用场景去配置GC ,让GC 发挥最好的作用..NET 5 改动更大,而且.NET 5整体性能比.net core 3.1高20%,并且在GC这 ...

  5. 【C#反射】BindingFlags 枚举

    BindingFlags 枚举用途:Type的类方法中,用于筛选成员. type.InvokeMember方法中 type.GetConstructor 方法中 type.GetFiles方法中 ty ...

  6. omnet++:官方文档翻译总结(二)

    这一部分是官方案例介绍 1.Introduction 学习自:Introduction - OMNeT++ Technical Articles 本教程是基于Tictoc的仿真案例,这些案例我们可以在 ...

  7. 文件上传漏洞之js验证

    0x00 前言 只有前端验证=没有验证 0x01 剔除JS 打开burpsuite,进入Proxy的Options,把Remove all JavaScript选上. 设置浏览器代理直接上传PHP木马 ...

  8. oracel数据库ORA-28001: the password has expired

    调试c#项目时登录用户不成功ORA-28001: the password has expired错误 密码过期失效 网上查了一下,是Oracle11g密码过期的原因 Oracle提示错误消息ORA- ...

  9. httpHelper 从URL获取值

    /// <summary> /// 从URL获取值(字符串) /// </summary> public static string GetValueFromUrl(strin ...

  10. MySQL第四讲

    昨日内容回顾 表与表之间建关系(外键) """ 表与表之间最多只有四种关系 一对多 多对多 一对一 没有关系 在确定表与表之间的关系的时候记住一句话 换位思考 " ...