SpringBoot applicationContext.getBeansOfType获取某一接口所有实现类,应用于策略模式

本文介绍了如何在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<>();
 
    @Override
    public 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 编码
     */
    @Override
    public TrafficCodeEmun getCode() {
        return TrafficCodeEmun.TRAIN;
    }
 
    /**
     * 查询交通方式的费用,单位:分
     * @return 费用
     */
    @Override
    public 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 对应的实现类没有注入进去,导致空指针的问题。
相关推荐
steel80885 分钟前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
lierenvip18 分钟前
Spring Boot中Tomcat配置
spring boot·tomcat·firefox
Detachym20 分钟前
InsightFlow 服务配置优化与部署实践
java·spring boot·tomcat·maven·状态模式·jar
rainchestnut20 分钟前
Spring AI 初步集成(1)-初始化
spring boot
y = xⁿ21 分钟前
【LeetCodehot100】T23:合并k个升序链表
java·数据结构·链表
流水武qin29 分钟前
SpringAI多模态的基本使用
java·spring boot·spring·ai
小飞Coding30 分钟前
Spring 容器生命周期:10大核心扩展接口+实战代码
spring boot
共享家952734 分钟前
Java入门(多态)
java·开发语言
毕设源码-赖学姐1 小时前
【开题答辩全过程】以 基于Java的婚礼策划平台的设计与实现为例,包含答辩的问题和答案
java·开发语言
吾诺1 小时前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端