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

原文地址:https://www.cnblogs.com/keeya/p/9331058.html

时间: 2024-08-04 13:44:23

Spring配置文件中的parent与abstract的相关文章

通过Spring配置文件中bean中的property赋值

基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用:可以把以前写dbc的东西写进去 2.如果属性的类型不是基本类型或String ,可以使用引用的方式为对象赋值(bean中property中的ref) 扩展-以此方式可以把数据库的连接值给实现类赋值 3.集合属性的赋值,注意要集合要初始化.基本数据类型不用初始化的原因就是它默认初始化(不常用) 4.

Spring中Bean的作用域、Spring的自动注入、在spring配置文件中引入属性文件

1. Bean的作用域 Bean的作用域默认为单例模式. 2. 自动注入 3. 在spring配置文件中引入属性文件 Bean的作用域默认为单例模式. 原文地址:https://www.cnblogs.com/mcl2238973568/p/11478426.html

在spring配置文件中的 &lt;context:property-placeholder/&gt;用途

location属性为 具体配置文件的classpath:地址 (可以取配置文件中的值利用${key}的形式,而不用多次写值) 1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource 在配置文件里这么定义bean: <bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManager

XML配置文件的命名空间与Spring配置文件中的头

一直以来,写Spring配置文件,都是把其他配置文件的头拷贝过来,最多改改版本号,也不清楚哪些是需要的,到底是干嘛的.今天整理一下,拒绝再无脑copy. 一.Spring配置文件常见的配置头 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/

Spring 配置文件中如何配置数据库连接

xml配置文件中配置如下:  <spring:bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <spring:property name="locations"> <spring:list> <spring:value>classp

[Spring] Spring配置文件中特殊字符的规定

今天查找一个错误,发现在xml里面不能包含特殊字符:&,特来总结一下: XML中共有5个特殊的字符,分别是:&<>“’.如果配置文件中的注入值包括这些特殊字符,就需要进行特别处理.有两种解决方法:其一,采用本例中的<![CDATA[ ]]>特殊标签,将包含特殊字符的字符串封装起来:其二,使用XML转义序列表示这些特殊的字符,这5个特殊字符所对应XML转义序列在表4-2中说明: Spring在进行XML配置时,如果属性值包含了一个XML的特殊符号,因此我们特意在属性值

Spring配置文件中直接定义bean时自动注入失败研究

一个Spring注入问题,首先看一个普通Spring Bean, public class Foo { @Autowired Bar bar; public void doSomething(){ bar.doSomething(); } } Spring配置一: <bean id="bar" class="com.test.Bar"></bean> <bean id="foo" class="com.te

Spring配置文件中突然出现异常Referenced file contains errors

Referenced file contains errors (http://cxf.apache.org/schemas/jaxws.xsd). For more information, right click on the message in the Problems View and select "Show Details..." 在Eclipse IDE中的一个J2EE工程,之前是好好的,一段时间没去搭理. 有一天需要在项目中加点东西,打开工程一看,其中一个Spring

Spring配置文件中注入复杂类型属性

Spring在整合其他框架时在配置文件中可能会涉及到复杂类型属性的注入,以下举例说明: 1.数组类型 2.List集合类型 3.Set集合类型 4.Map集合类型 5.Properties属性类型 直接上代码: 实体类:CollectionBean.java package com.imooc.ioc.demo5; import java.util.*; /** * Spring复杂类型的集合注入 */ public class CollectionBean { private String[]