本文介绍了如何在Springboot项目中通过ApplicationContext获取接口的实现类,并通过枚举策略模式避免if/else,展示了如何使用`getBeansOfType`获取`TrafficModeService`的实现,以及如何在实际场景中应用,如查询交通方式费用
1 在实际工作中,我们经常会遇到一个接口及多个实现类的情况,并且在不同的条件下会使用不同的实现类。
应用场景
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
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、定义策略接口 TrafficModeServicepublic 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;}注意点:
一个策略接口被多个策略实现类所实现,具体使用哪一种根据用户选择的类型来和 Map 里的 key 做匹配,获取对应的实现来调用具体的策略方法。
使用 ConcurrentHashMap ,而不使用 HashMap ,是 put 的时候,键和值都不能为空,防止 key 对应的实现类没有注入进去,导致空指针的问题。