Spring可自动将多个接口实现类以Bean名称为key、实例为value注入Map<String, Interface>。需确保实现类被扫描到、Map值类型为接口、Bean名称唯一,支持按名获取或遍历调用。
怎么按 bean 名字收集">
在 Spring 中,若想按 Bean 名称收集某个接口的多个实现类(比如 Service 接口有多个实现),并以 Map<string service></string> 形式注入,只需让 Spring 容器自动装配即可——不需要手动注册或额外配置。
Spring 会将所有类型为 Service 的 Bean,以其 Bean 名称(即 bean id)作为 key、对应实例作为 value,自动组装进 Map<string service></string>。前提是该 Map 的 value 类型必须是具体接口或父类(如 Service),且容器中存在多个匹配的 Bean。
OrderServiceImpl → orderServiceImpl)@Service("customName") 或 @Bean("customName") 指定了名称,则以该名称为准假设有接口 PaymentService 和两个实现:
@Service("alipayService")public class AlipayPaymentServiceImpl implements PaymentService { ... }@Service("wechatService")public class WechatPaymentServiceImpl implements PaymentService { ... }
在需要使用的类中直接注入:
立即学习“Java免费学习笔记(深入)”;
@Autowiredprivate Map<String, PaymentService> paymentServiceMap;
此时 paymentServiceMap 包含:{"alipayService": AlipayPaymentServiceImpl@xxx, "wechatService": WechatPaymentServiceImpl@xxx}
确保能成功注入,需注意以下几点:
@Service / @Component 等注解,且包路径在 @ComponentScan 范围内)PaymentService),不能是 Object 或 Object 子类(否则无法类型匹配)@Primary,不影响 Map 注入,它仍会出现在 Map 中(@Primary 只影响单个 Bean 注入场景)ApplicationContext.getBeansOfType(PaymentService.class) 验证有了 Map 后,可灵活调用:
paymentServiceMap.forEach((name, impl) -> impl.pay(...))
PaymentService wechat = paymentServiceMap.get("wechatService");
String type = config.getPaymentType(); paymentServiceMap.get(type).pay(...)