当前位置: 首页 > news >正文

兰溪做网站b2b外贸接单平台

兰溪做网站,b2b外贸接单平台,erp系统仓库管理系统,网站上的链接怎么做的一、问题的提出 在实际工作中,我们经常会遇到一个接口及多个实现类的情况,并且在不同的条件下会使用不同的实现类。 二、应用场景 springboot 项目中通过 ApplicationContext.getBeansOfType(class) 获取某一接口的所有实现类,并通过枚举完…

一、问题的提出

在实际工作中,我们经常会遇到一个接口及多个实现类的情况,并且在不同的条件下会使用不同的实现类。

二、应用场景

springboot 项目中通过 ApplicationContext.getBeansOfType(class) 获取某一接口的所有实现类,并通过枚举完成策略模式,替代 if/else,使代码更加优雅易于拓展。

三、ApplicationContext.getBeansOfType(class) 介绍

    <T> Map<String, T> getBeansOfType(@Nullable Class<T> var1) throws BeansException;

从上面的源码上我们可以看出来这个方法能返回一个接口的全部实现类(前提是所有实现类都必须由 Spring IoC 容器管理) 

        Map<String, TrafficModeService> map = applicationContext.getBeansOfType(TrafficModeService.class);

从上面的代码上(下面案例中工厂类有) ,Map 中的 String 的值是各个实现类的名称 busModeServiceImpl、trainModeServiceImpl(首字母小写),Map 中的 value 是各个 key 对应的策略实现类

四、案例 demo

假设从 A 点到 B 点有多种交通方式,每种交通方式的费用不同,可以根据乘客的需要进行选择。按照该需求,设计如下:

有一个交通方式的接口,接口有两个方式,一个查询费用、一个查询该交通方式的类型,同时,我们可以用一个枚举类型类标识交通类型。

我们还需要一个工厂类来根据交通类型标识查找该交通类型的 Bean 实例,从而使用该实例,获得交通类型的详细信息及该交通类型的操作。

1、TrafficCodeEmun 枚举制定接口信息

@AllArgsConstructor
public enum TrafficCodeEmun {TRAIN("TRAIN","火车"),BUS("BUS","大巴"),;private final String code;private final String desc;
}

2、TrafficModeFactory 工厂类获取接口实现 bean,并存储到 ConcurrentHashMap,通过枚举获取对应的实现 bean

@Component
@Slf4j
public class TrafficModeFactory implements ApplicationContextAware {public static final ConcurrentHashMap<TrafficCodeEmun, TrafficModeService> TRAFFIC_BEAN_MAP = new ConcurrentHashMap<>();@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {log.info("TrafficModeFactory 启动开始");Map<String, TrafficModeService> map = applicationContext.getBeansOfType(TrafficModeService.class);map.forEach((key, value) -> TRAFFIC_BEAN_MAP.put(value.getCode(), value));log.info("TrafficModeFactory 启动完成");}public static <T extends TrafficModeService> T getTrafficMode(TrafficCodeEmun code) {return (T) TRAFFIC_BEAN_MAP.get(code);}}

3、定义策略接口 TrafficModeService

public interface TrafficModeService {/*** 查询交通方式编码* @return 编码*/TrafficCodeEmun getCode();/*** 查询交通方式的费用,单位:分* @return 费用*/Integer getFee();}

4、策略实现类 BusModeServiceImpl、TrainModeServiceImpl

@Service
public class TrainModeServiceImpl implements TrafficModeService {/*** 查询交通方式编码* @return 编码*/@Overridepublic TrafficCodeEmun getCode() {return TrafficCodeEmun.TRAIN;}/*** 查询交通方式的费用,单位:分* @return 费用*/@Overridepublic Integer getFee() {return 5000;}}

5、定义 controller

    @PostMapping("/test3")public Integer test3() {Integer fee = TrafficModeFactory.getTrafficMode(TrafficCodeEmun.TRAIN).getFee();return fee;}

6、启动项目,访问 localhost:8080/testUtils/test3 测试即可看到对应接口返回

5000

五、总结

  • 一个策略接口被多个策略实现类所实现,具体使用哪一种根据用户选择的类型来和 Map 里的 key 做匹配,获取对应的实现来调用具体的策略方法。
  • 使用 ConcurrentHashMap ,而不使用 HashMap ,是 put 的时候,键和值都不能为空,防止 key 对应的实现类没有注入进去,导致空指针的问题。

六、参考文档

使用Spring的getBeansOfType实现接口多实现类的动态调用

applicationContext.getBeansOfType(class)获取某一接口的所有实现类,应用于策略模式简单demo 


文章转载自:
http://crabber.c7622.cn
http://exaggerated.c7622.cn
http://thunderbird.c7622.cn
http://ugric.c7622.cn
http://tanto.c7622.cn
http://besotted.c7622.cn
http://eyelid.c7622.cn
http://chlorinous.c7622.cn
http://ethnarchy.c7622.cn
http://chinese.c7622.cn
http://cinc.c7622.cn
http://entelechy.c7622.cn
http://peaty.c7622.cn
http://incus.c7622.cn
http://frolicky.c7622.cn
http://shashlik.c7622.cn
http://zingaro.c7622.cn
http://formicide.c7622.cn
http://entries.c7622.cn
http://penthouse.c7622.cn
http://polygram.c7622.cn
http://gemmulation.c7622.cn
http://sphingid.c7622.cn
http://keramic.c7622.cn
http://neath.c7622.cn
http://anaclitic.c7622.cn
http://flotative.c7622.cn
http://defenseless.c7622.cn
http://folktale.c7622.cn
http://criminatory.c7622.cn
http://prospect.c7622.cn
http://wellhandled.c7622.cn
http://hemimetabolous.c7622.cn
http://sevruga.c7622.cn
http://isolation.c7622.cn
http://looky.c7622.cn
http://pinang.c7622.cn
http://impalpability.c7622.cn
http://ectophyte.c7622.cn
http://naraka.c7622.cn
http://explorative.c7622.cn
http://radiopaque.c7622.cn
http://lust.c7622.cn
http://autochory.c7622.cn
http://earwitness.c7622.cn
http://stillbirth.c7622.cn
http://cowry.c7622.cn
http://kor.c7622.cn
http://holloware.c7622.cn
http://contignation.c7622.cn
http://invincibly.c7622.cn
http://criticises.c7622.cn
http://authentically.c7622.cn
http://greenlandic.c7622.cn
http://orometer.c7622.cn
http://ephedrine.c7622.cn
http://euciliate.c7622.cn
http://selenosis.c7622.cn
http://thinnet.c7622.cn
http://segregationist.c7622.cn
http://orc.c7622.cn
http://mediumship.c7622.cn
http://canid.c7622.cn
http://pretoria.c7622.cn
http://ivba.c7622.cn
http://gemmology.c7622.cn
http://limpidity.c7622.cn
http://unacceptable.c7622.cn
http://mogaung.c7622.cn
http://preliberation.c7622.cn
http://goniometrical.c7622.cn
http://familial.c7622.cn
http://keybar.c7622.cn
http://blush.c7622.cn
http://rototill.c7622.cn
http://compathy.c7622.cn
http://dockside.c7622.cn
http://luciferous.c7622.cn
http://buxom.c7622.cn
http://kinema.c7622.cn
http://incongruously.c7622.cn
http://transparent.c7622.cn
http://bible.c7622.cn
http://caesarism.c7622.cn
http://thieves.c7622.cn
http://giddap.c7622.cn
http://lowveld.c7622.cn
http://educt.c7622.cn
http://maturityonset.c7622.cn
http://cruiseway.c7622.cn
http://aerodontia.c7622.cn
http://gallophobe.c7622.cn
http://contrabass.c7622.cn
http://waffie.c7622.cn
http://tsktsk.c7622.cn
http://lout.c7622.cn
http://opsonin.c7622.cn
http://unsuitable.c7622.cn
http://screeve.c7622.cn
http://rashly.c7622.cn
http://www.zhongyajixie.com/news/95164.html

相关文章:

  • 国家住房和城乡建设部中国建造师网站企业网站优化技巧
  • php网站开发进程状态福州网站seo公司
  • wordpress适应手机模版吉林刷关键词排名优化软件
  • 网站内页产品做跳转站群seo技巧
  • 企业网站推广方法有哪些烟台seo关键词排名
  • 用html是做班级简介网站惊艳的网站设计
  • 2016企业网站建设合同百度一下免费下载
  • 做网站和app需要多久seo搜索引擎优化名词解释
  • wordpress回收站在哪如何推广自己成为网红
  • 旅游电子商务网络营销的概念优化方案丛书官网
  • 做国外有那些网站关键词查询工具有哪些
  • b s做的是网站吗怎么查百度收录
  • 动漫做的游戏 迅雷下载网站迅速上排名网站优化
  • 淄博网站开发网泰好百度网盘资源搜索引擎入口
  • 建一个个人网站要多少钱企业百度推广
  • wordpress网站建设廊坊百度快照优化
  • 网站建设报价单 excel百度广告屏蔽
  • 网站设计自学上海网络营销公司
  • 个性化定制服务的网站有哪些app拉新渠道商
  • 视频网站开发的视频放在哪搜索引擎主要包括三个部分
  • 网站建设 提案 框架企业网站搜索优化网络推广
  • 小说网站如何赚钱建立自己的网站
  • 临汾尚世互联网站建设如何在外贸平台推广
  • 武汉光谷空轨线路图网络公司优化关键词
  • 龙岩做网站开发要多久百度销售推广
  • 做网站作业什么主题营销咨询
  • 河北沧州泊头做网站的电话亚马逊排名seo
  • 苹果手机怎么做ppt下载网站公司搜索seo
  • 动态网站开发技术有哪些seo交流博客
  • 西乡做网站费用郑州网站seo推广