关于 java 多继承的典型应用实例 针对不同业务类型的XML文件的解析

在PCS 项目:

public class CainiaoXMLMessageResolverServiceImpl implements CainiaoXMLMessageResolverService,InitializingBean {

CainiaoXMLMessageResolverServiceImpl 如下:

1.>>public interface CainiaoMessageResolver{

ResultMessage resolveMessage(CainiaoMessageEntity o);
CainiaoMessageEventTypeEnum getAcceptType();

Class getAcceptEntity();

}

实现此接口的类:

java 多继承的典型应用实例(不同的报文不同的方法去解析)-LMLPHP

2. @Qualifier("XXX") Spring的Bean注入配置注解,该注解指定注入的Bean的名称,Spring框架使用byName方式寻找合格的bean,这样就消除了byType方式产生的歧义。(@Qualifier 用法见以下介绍

@Qualifier("logisticsDispatchedMessageResolver")
@Resource
private CainiaoMessageResolver logisticsDispatchedMessageResolver;

3.

private static Map<CainiaoMessageEventTypeEnum,CainiaoMessageResolver> map = new HashMap<CainiaoMessageEventTypeEnum,CainiaoMessageResolver>();

@Override
public void afterPropertiesSet() throws Exception {
map.put(logisticsDispatchedMessageResolver.getAcceptType(),logisticsDispatchedMessageResolver);
map.put(cancelLogisticsInfoMessageResolver.getAcceptType(),cancelLogisticsInfoMessageResolver);
map.put(cancelOrderMessageResolver.getAcceptType(),cancelOrderMessageResolver);
map.put(globalLinehaulAsnMessageResolver.getAcceptType(),globalLinehaulAsnMessageResolver);
map.put(logisticsSecDispatchedResolver.getAcceptType(),logisticsSecDispatchedResolver);
map.put(logisticsCombinedPaidMessageResolver.getAcceptType(),logisticsCombinedPaidMessageResolver);
map.put(logisticsUpdateInfoMessageResolver.getAcceptType(),logisticsUpdateInfoMessageResolver);
map.put(tradePaidMessageResolver.getAcceptType(),tradePaidMessageResolver);
map.put(transitWarehouseBoundoutNoticeMessageResolver.getAcceptType(),transitWarehouseBoundoutNoticeMessageResolver);
map.put(consoWarehouseConsignMessageResolver.getAcceptType(),consoWarehouseConsignMessageResolver);
map.put(consoWarehouseOutboundNoticeMessageResolver.getAcceptType(),consoWarehouseOutboundNoticeMessageResolver);
map.put(consoWarehouseCancelConsignMessageResolver.getAcceptType(),consoWarehouseCancelConsignMessageResolver);
map.put(consoWarehouseFeeStatusNoticeMessageResolver.getAcceptType(),consoWarehouseFeeStatusNoticeMessageResolver);
map.put(consoWarehouseUpdateConsignMessageResolver.getAcceptType(),consoWarehouseUpdateConsignMessageResolver);
map.put(consoWarehouseReturnRefundNoticeMessageResolver.getAcceptType(),consoWarehouseReturnRefundNoticeMessageResolver);
PCSAssert.assertFalse(CainiaoMessageEventTypeEnum.values().length != map.size(), "err110110", "有部分报文类型未注册解析处理器!");
}

说明 此类型实现 InitializingBean  覆写 afterPropertiesSet()方法。 在spring boot 初始化阶段 会调用 afterPropertiesSet()方法。

3. 取xml 业务类型代码:

private static CainiaoMessageEventTypeEnum getDataGramType(String xml) {
Document document = null;
CainiaoMessageEventTypeEnum type = null;
try {
document = DocumentHelper.parseText(xml);
Node eventType = document.selectSingleNode("//logisticsEvent/eventHeader/eventType");
if (eventType != null && StringUtils.isNotBlank(eventType.getText())) {
type = CainiaoMessageEventTypeEnum.valueOf(eventType.getText().trim());
}

} catch (DocumentException e) {
LOG.error("解析失败的报文:{}",xml);
return null;

}

return type;
}

4.得到不同的类型处理

resolver = map.get(datagramType);
if(resolver == null){
LOG.error("未获取到报文处理方法,报文类型:{},报文内容:{}",datagramType,xml);
presult = false;

}
else{
Class clazz = resolver.getAcceptEntity();
Object obj = xmlOperatorInterface.toBean(xml, clazz);
xmlObj =(CainiaoMessageEntity)obj;

5.找到对应业务的处理类的下的方法

for(int i =0;i<3;i++){
try{
resolveResult=resolver.resolveMessage(xmlObj);
errcode = resolveResult.getCode();
if( ! ResultMessage.RESULT_SUCCESS.equals(errcode)){
errmsg = resolveResult.getMessage();
}
succ = true;
break;
}catch(Exception ex){
if(i == 2){
e = ex;
LOG.error(e.getMessage(),e);
}
}
}

备注:关键写法:自己不知道的

@Override
public Class<CancelLogisticsInfo> getAcceptEntity() {
return CancelLogisticsInfo.class;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@Qualifier("XXX") Spring的Bean注入配置注解,该注解指定注入的Bean的名称,Spring框架使用byName方式寻找合格的bean,这样就消除了byType方式产生的歧义。

Spring的IoC的xml配置文件方式会让XML文件变的比较长而且繁琐,所以自JDK 1.5之后,越来越多的框架推出注解配置,

Spring的Bean注入就可以通过@Resource、@Autowire标记此处要注入bean,Spring框架通过反射方式给字段注入合适的对象。

@Autowire注解按照类型,即注解的字段的类型寻找该类型的实例bean,这种方式成为byType。这种方式会引发歧义,比如UserDAO整个框架可能有N多个该对象,那么Spring框架会采用一定规则寻找bean(转换为byName寻找,失败后报错)

@Resource是JDK提供的注解,默认就是按照byName的方式寻找bean,一般一个name对应一个bean,当找不到与名称匹配的bean才会按照类型装配(byType)。

想要让@Autowired方式按照name方式注入,可以结合@Qualifier("XX")使用,让@Autowired按照byName方式装配。

-------------------------------------------------------------------------------附备注-------------------------------------------------------------------

package com.fpx.order.util.xml;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
* XML解析,转换工具类
* @author xufeng
* @since 2017.7.9
* @version 1.1.0
*/
public interface XmlOperatorInterface {

public <T> T toBean(String xmlStr, Class<T> cls);

/**
* XStream技术转换对象为xml字符串
* @param obj 类声明上必须有XStream注解
* @return
*/
public String toXmlStr(Object obj);
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

java 多继承的典型应用实例(不同的报文不同的方法去解析)-LMLPHP

05-22 20:29