Is there an easy way to autowire empty collection if no beans present in Spring?

后端 未结 2 1661
情歌与酒
情歌与酒 2021-01-03 18:38

If I have @Autowired List beans; and no beans of SomeBeanClass, I get:

No matching bean of type [SomeBe

2条回答
  •  旧巷少年郎
    2021-01-03 18:52

    If I add (required=false), I get null for beans.

    Does the field get explicitly set to null or does it simply not get set at all? Try adding an initializer expression

    @Autowired List beans = new ArrayList<>();
    

提交回复
热议问题