spring对集合类型进行注入

如果对类中集合类的变量或方法入参进行@Autowired标注,那么spring会将容器中类型匹配的所有Bean都自动注入进来。

@Component
public class MyComponent{
    //spring会将容器中所有类型为Apple的Bean诸如这个变量中
    @Autowered
    private List<Apple> apples

    @Autowired
    private Map<String,Apple> appleMaps

    public list<apple> getApples(){
        return apples;
    }
}
spring如果发现变量是一个List和一个Map集合,则他会将容器中匹配集合元素类型的所有Bean都注入进来,Map中key是Bean的名字,value是所有Apple Bean

猜你喜欢

转载自blog.csdn.net/hello_it_/article/details/79422549
今日推荐