Spring @Configuration继承
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继承的更多相关文章
- Spring Configuration Check Unmapped Spring configuration files found
Spring Configuration Check Unmapped Spring configuration files found 项目中有xml文件,但没有被用IntelliJ 导入现有工程时 ...
- IntelliJ 15 unmapped spring configuration files found
IntelliJ Spring Configuration Check 用IntelliJ 导入现有工程时,如果原来的工程中有spring,每次打开工程就会提示:Spring Configuratio ...
- Spring @Configuration 和 @Component 区别
Spring @Configuration 和 @Component 区别 一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个 ...
- 出现unmapped spring configuration files found
intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.
- IntelliJ IDEA 2017 提示“Unmapped Spring configuration files found.Please configure Spring facet.”解决办法
当把自己的一个项目导入IDEA之后,Event Log提示“Unmapped Spring configuration files found.Please configure Spring face ...
- "Unmapped Spring configuration files found.Please configure Spring facet."解决办法
最近在学习使用IDEA工具,觉得与Eclipse相比,还是有很多的方便之处. 但是,当把自己的一个项目导入IDEA之后,Event Log提示"Unmapped Spring configu ...
- io.spring.platform继承方式和import方式更改依赖版本号的问题
使用io.spring.platform时,它会管理各类经过集成测试的依赖版本号. 但有的时候,我们想使用指定的版本号,这个时候就需要去覆盖io.spring.platform的版本号. 前面的文章总 ...
- spring configuration 注解
org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...
- Spring中继承配置的注入方法
(1)两个java类.一个父类一个字类 package com.lc.inherit; /* * 这里是父类 */ public class Student { protected String na ...
随机推荐
- Internet/Custom路由配置——网络测试仪实操
一.测试说明以及功能原理 本文主要介绍Internet/Custom路由配置方法以及实验: ◆作用:可以通过此功能模拟注入不同百分比掩码的路由 ◆特点:只针对路由协议(目前RIP协议不支持,因为RIP ...
- SQL Server Cross/Outer Apply
SQL Server2005引入了APPLY运算符,它非常像连接子句,它允许两个表达式直接进行连接,即将左/外部表达式和右/内部表达式连接起来. CROSS APPLY(类比inner join)和O ...
- 小白文-SpringMVC-解读DispatcherServlet源码
SpringMVC 学习完Spring框架技术之后,差不多会出现两批人: 一批是听得云里雾里,依然不明白这个东西是干嘛的: 还有一批就是差不多理解了核心思想,但是不知道这些东西该如何去发挥它的作用. ...
- Spring Cloud Alibaba 2021.0.1.0 发布:版本号再也不迷糊了
大家好,DD又来了! 3月9日,Spring官方博客发文:Spring Cloud Alibaba 2021.0.1.0发布了. 前段时间DD还在微信群里看到小伙伴吐槽Spring Cloud Ali ...
- tp5 终端命令总结
D:\PHP\phpstudy_pro\WWW\1906A\tp5>php think build --module examTest Successed D:\PHP\phpstudy_pro ...
- php 23种设计模型 - 单例模式
单例模式(Singleton) 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一 ...
- 被广泛使用的OAuth2.0的密码模式已经废了,放弃吧
最近一直有同学在问,OAuth2密码模式为啥Spring Security还没有实现,就连新的Spring Authorization Server也没有这个玩意儿. 其实这里可以告诉大家,OAuth ...
- 记一次CentOS根目录空间占满的解决方案
问题背景是这样的,根目录初始大小是50G,我把mysql和docker都装在根目录底下了. 随着时间推移,mysql大小达到了27个G,docker则是22个G 根目录空间不够用了,上图: 图 1-1 ...
- 《Shader入门精要》第11章-11.3.1流动的河流中的offset.x的解释
在我学习入门精要的时候,经常遇到不解释api,甚至是关键代码的实现原理. 11.3.1流动的河流中的offset.x的sin函数查了一下好像大家也都是书上原话直接复制,现在好不容易想明白了希望能帮到和 ...
- ArcMap操作随记(1)
1.用python代码,将属性表中属性进行合并 代码:'ID'+str(!FID!) 2.地类编码与地类的转换 以python为例 显示代码块(预逻辑脚本代码): def getType(value) ...