@Autowired private Map<String, T> map = new HashMap<>();// 向map中注入spring容器中所有类型为T类型的bean,其中键为bean的名称,值为bean的实例. 比如 value为 BizService对象,那么key就是bizService
1.创建父接口
public interface SendMsgProvider {
}
2.分别创建三个实现类
@Service
public class AliSendMsgProviderImpl implements SendMsgProvider{
}
@Service
public class BaoliSendMsgProviderImpl implements SendMsgProvider{
}
@Service
public class NullSendMsgProviderImpl implements SendMsgProvider{
}
3.控制层的方法
@RestController
@RequestMapping("")
public class ActiveController {
@Autowired
private Map<String, SendMsgProvider> sendMsgProviderMap;
@GetMapping("")
public void test(){
System.out.println(sendMsgProviderMap.getClass().getName());
for(Map.Entry<String,SendMsgProvider> sendMsgProviderMap.entrySet()){
System.out.println(map.getKey()+"=========>"+map.getValue().getClass().getName());
}
}
}
4.启动程序并使用postMan调用这个接口,看控制台输出
java.util.LinkedHashMap
experienceAct=========>com.sms.ActivityDesign.AliSendMsgProviderImpl
groupAct=========>com.sms.ActivityDesign.BaoliSendMsgProviderImpl
inviteCardAct=========>com.sms.ActivityDesign.NullSendMsgProviderImpl
|