Autowire all beans for a specific type
这篇文章是参考 【spring自动注入接口的多个实现类(结合策略设计模式)】 得来的。
按里面的内容敲了一下代码,总是有错误,然后自己又搜索一下,得到了正确结果:
image.png
图片来源:https://www.baeldung.com/spring-dynamic-autowire
完整代码如下:
策略接口:
public interface DiscountStrategy {
String type();
double discount(double fee);
}
策略具体实现类:
三个实现类
调用接口:
public interface DiscountStrategyService {
double discount(String type, double fee) ;
}
接口实现:
@Service
public class DiscountStrategyServiceImpl implements DiscountStrategyService {
private final Map map;
// Besides standard single-field autowiring,
// Spring gives us an abil