Spring配置文件中的parent与abstract
在看项目的Spring配置文件时,发现消息队列的配置采用了继承方式配置Bean,在这梳理总结一下。
其实在基于spring框架开发的项目中,如果有多个bean都是一个类的实例,如配置多个数据源时,大部分配置的属性都一样,只有少部分不一样。这样的话在配置文件中可以配置和对象一样进行继承。
例如
<bean id="testParent" abstract="true" class="com.bean.TestBean">
<property name="param1" value="父参数1"/>
<property name="param2" value="父参数2"/>
</bean>
<bean id="testBeanChild1" parent="testParent"/>
<bean id="testBeanChild2" parent="testParent">
<property name="param1" value="子参数1"/>
</bean>
- 其中 abstract="true" 的配置表示:此类在Spring容器中不会生成实例。
- parent="testBeanParent" 代表子类继承了testBeanParent,会生成具体实例,在子类Bean中配置会覆盖父类对应的属性。
在博主项目中遇到场景是这样的:
在Spring配置文件中配置消息队列中消费者的实现类,但其实每个消费者仅仅有个几个属性不相同,为了避免代码冗余、修改和可扩展,采用了上述的方式进行配置。
配置如下:
order-task-comsumer.xml
父类中将消息队列所以要的各种key和url配置全,子类仅仅只有几个属性不同。
如果以后非淘平台的业务膨胀了,仅仅只要在配置文件中拆出部分,很方便。
<!-- 自动下单任务监听 -->
<bean id="abstractAutoFetchOrderTaskMessageListener" abstract="true" class="com.foonsu.erp.orderpool.listeners.AutoFetchOrderTaskMessageListener">
<property name="accessKey" value="${taobao.appKey}" />
<property name="secretKey" value="${taobao.appSecret}" />
<property name="onsChannel" value="${mq.erp_orderpool.autoFetchOrder.onsChannel}" />
<property name="messageModel" value="${mq.erp_orderpool.autoFetchOrder.messageModel}" />
<property name="topic" value="${mq.erp_orderpool.autoFetchOrder.topic}" />
<property name="messageOrder" value="${mq.erp_orderpool.autoFetchOrder.messageOrder}" />
<property name="orderPoolTaskHandlers" ref="orderPoolTaskHandlersMap"/>
</bean>
<!--消费淘宝平台消息的消费者 -->
<bean id="taobaoAutoFetchOrderTaskMessageListener" parent="abstractAutoFetchOrderTaskMessageListener" >
<property name="consumerId" value="${mq.erp_orderpool.autoFetchOrder.taobaoConsumerId}" />
<property name="subExpression" value="${mq.erp_orderpool.autoFetchOrder.taobaoSubExpression}" />
<property name="consumeThreadNums" value="${mq.erp_orderpool.autoFetchOrder.taobaoConsumeThreadNums}" />
</bean>
<!--消费非淘宝平台消息的消费者 -->
<bean id="otherAutoFetchOrderTaskMessageListener" parent="abstractAutoFetchOrderTaskMessageListener" >
<property name="consumerId" value="${mq.erp_orderpool.autoFetchOrder.otherConsumerId}" />
<property name="subExpression" value="${mq.erp_orderpool.autoFetchOrder.otherSubExpression}" />
<property name="consumeThreadNums" value="${mq.erp_orderpool.autoFetchOrder.otherConsumeThreadNums}" />
</bean>
<bean id="orderPoolTaskHandlersMap" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="01" value-ref="taoBaoOrderTaskHandler" />
<entry key="05" value-ref="jdOrderTaskHandler" />
<entry key="25" value-ref="dadangjiaOrderTaskHandler" />
<entry key="16" value-ref="pddOrderTaskHandler" />
<entry key="13" value-ref="alibabaOrderTaskHandler" />
</map>
</constructor-arg>
</bean>
ps:${XXX} 配置中类似这样的符号表示是从properties文件中读取需要的配置,Spring加载的时候会替换。
alibaba.properties
alibaba.appKey=7151192
alibaba.appSecret=vLCR5tk0D5u7
alibaba.dataUrl=http://gw.open.1688.com/openapi/
alibaba.accessTokenUrl=https://gw.open.1688.com/openapi/http/1/system.oauth2/getToken/7151192
alibaba.refreshTokenUrl=https://gw.open.1688.com/openapi/param2/1/system.oauth2/getToken/7151192
alibaba.logisticsCompanyCodeMapStr=01\=SF,\u987a\u4e30|02\=EMS,EMS|03\=STO,\u7533\u901a|04\=HTKY,\u767e\u4e16\u5feb\u9012|05\=YTO,\u5706\u901a|06\=ZTO,\u4e2d\u901a|07\=YUNDA,\u97f5\u8fbe|08\=EYB,EMS\u7ecf\u6d4e\u5feb\u9012|09\=ZJS,\u5b85\u6025\u9001|10\=OTHER,\u5176\u5b83|11\=UC,\u4f18\u901f|12\=TTKDEX,\u5929\u5929|13\=QFKD,\u5168\u5cf0\u5feb\u9012|14\=FAST,\u5feb\u6377\u901f\u9012|15\=POSTB,\u90ae\u653f\u56fd\u5185\u5c0f\u5305|16\=GTO,\u56fd\u901a\u5feb\u9012|17\=DBKD,\u5fb7\u90a6\u5feb\u9012|18\=EMS,EMS|19\=OTHER,\u5176\u5b83|20\=OTHER,\u5176\u5b83|21\=ANE56,\u5b89\u80fd\u7269\u6d41|22\=OTHER,\u5176\u5b83|23\=OTHER,\u5176\u5b83|24\=SURE,\u901f\u5c14|25\=OTHER,\u5176\u5b83
Spring配置文件中的parent与abstract的更多相关文章
- Spring配置文件中使用ref local与ref bean的区别
Spring配置文件中使用ref local与ref bean的区别.在ApplicationResources.properties文件中,使用<ref bean>与<ref lo ...
- 通过Spring配置文件中bean中的property赋值
基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用 ...
- Spring配置文件中未引入dubbo命名空间头部配置而引起的错误案例
问题描述: Spring配置文件中未引入dubbo命名空间的头部配置而引起项目启动时报出如下错误信息: org.springframework.beans.factory.xml.XmlBeanDef ...
- Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件
1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...
- 在spring配置文件中引入外部properties配置文件 context:property-placeholder
在spring的配置文件中,有时我们需要注入很多属性值,这些属性全都写在spring的配置文件中的话,后期管理起来会非常麻烦.所以我们可以把某一类的属性抽取到一个外部配置文件中,使用时通用spring ...
- Spring 源码(4)在Spring配置文件中自定义标签如何实现?
Spring 配置文件自定义标签的前置条件 在上一篇文章https://www.cnblogs.com/redwinter/p/16165274.html Spring BeanFactory的创建过 ...
- [Spring] Spring配置文件中特殊字符的规定
今天查找一个错误,发现在xml里面不能包含特殊字符:&,特来总结一下: XML中共有5个特殊的字符,分别是:&<>“’.如果配置文件中的注入值包括这些特殊字符,就需要进行特 ...
- XML配置文件的命名空间与Spring配置文件中的头
一直以来,写Spring配置文件,都是把其他配置文件的头拷贝过来,最多改改版本号,也不清楚哪些是需要的,到底是干嘛的.今天整理一下,拒绝再无脑copy. 一.Spring配置文件常见的配置头 < ...
- Spring配置文件中如何使用外部配置文件配置数据库连接
直接在spring的配置文件中applicationContext.xml文件中配置数据库连接也可以,但是有个问题,需要在url后带着使用编码集和指定编码集,出现了如下问题,&这个符号报错-- ...
随机推荐
- CentOS 7_64位系统下搭建Hadoop_2.8.0分布式环境
准备条件: CentOS 7 64位操作系统 | 选择minimal版本即可(不带可视化桌面环境),也可以选择带完整版Hadoop-2.8.0 | 本文采用的是Hadoop-2.8.0版本.JDK1. ...
- Tomcat源码分析——server.xml文件的加载
前言 作为Java程序员,对于tomcat的server.xml想必都不陌生.本文基于Tomcat7.0的Java源码,对server.xml文件是如何加载的进行分析. 源码分析 Bootstrap的 ...
- Angular 应用中的登陆与身份验证
Angular 经常会被用到后台和管理工具的开发,这两类都会需要对用户进行鉴权.而鉴权的第一步,就是进行身份验证.由于 Angular 是单页应用,会在一开始,就把大部分的资源加载到浏览器中,所以就更 ...
- jquery点击按钮或链接,第一次与第二次执行不同的事件
本文和大家分享一个jquery的实例,这个实例实现的是点击网页里的按钮或链接,第一次和第二次会执行不同的事件,也就是两个事件会轮流执行. <script language="javas ...
- unity项目git管理
Unity设置 (关键) Edit -> Project Settings -> Editor -> Version Control Mode 开启 Visible Meta Fil ...
- HTML5--(2)属性选择器+结构性伪类+伪类
一.属性选择器 [att] 匹配所有具有att属性的 [att=val] 匹配所有att属性等于“val”的 [att~=val] 匹配所有att属性包含“val”或者等于“val”的(val必须是一 ...
- 第7天:javascript-DOM 获取标签、注册事件改变属性的值、innerText、改变属性的值等
javascript WEB api------DOM document object model 案例 为元素注册点击事件,弹出对话框 <input type="button&quo ...
- 微服务系列(二):使用 API 网关构建微服务
编者的话|本文来自 Nginx 官方博客,是微服务系列文章的第二篇,本文将探讨:微服务架构是如何影响客户端到服务端的通信,并提出一种使用 API 网关的方法. 作者介绍:Chris Richardso ...
- spring data jpa(一)
第1章 Spring Data JPA的快速入门 1.1 需求说明 Spring Data JPA完成客户的基本CRUD操作 1.2 搭建Spring Data JPA的开发环境 1. ...
- composer gitlab 搭建私包
一.建立私包git 1.执行composer init 根据提示生成composer.json 2.编辑composer.json 目录格式 { "name": "iar ...