一个接口有多个实现类,怎么让springboot自动决定执行哪个实现类的代码

@Component
public class ImplementSubformFactory implements ApplicationContextAware {
private static Map<String, ImplementSubformService> productMap = new HashMap<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//获取ImplementSubformService的所有实现类
Map<String, ImplementSubformService> map = applicationContext.getBeansOfType(ImplementSubformService.class);
for (Map.Entry<String, ImplementSubformService> data : map.entrySet()) {
//在实现类上看有没有使用注解@ImplementSubform
ImplementSubformService subform = data.getValue();
Class<? extends ImplementSubformService> aClass = subform.getClass();
ImplementSubform annotation = AnnotationUtils.findAnnotation(aClass, ImplementSubform.class);
if (null != annotation) {
//有使用,则将注解的type值作为key,实现类作为value存到全局的map中
productMap.put(annotation.type(), subform);
}
}
}
//在业务中,直接根据type值就可以拿到具体实现类,然后就执行具体的代码了
public static ImplementSubformService makeProduct(String name) {
return productMap.get(name);
}
}
posted @   晚风没有颜色  阅读(520)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示