Auto-wiring a List using util schema gives NoSuchBeanDefinitionException
Solution 1:
This is due to a rather obscure part of @Autowired's behaviour, specified in 3.11.2. @Autowired:
It is also possible to provide all beans of a particular type from the
ApplicationContext
by adding the annotation to a field or method that expects an array of that type...The same applies for typed collections...
In other words, by saying @Autowired @Qualifier("myList") List<String>
, you're actually asking for "give me the list of all beans of type java.lang.String
that have the qualifier "myList".
The solution is mentioned in 3.11.3. Fine-tuning annotation-based autowiring with qualifiers:
If you intend to express annotation-driven injection by name, do not primarily use
@Autowired
- even if is technically capable of referring to a bean name through@Qualifier
values. Instead, prefer the JSR-250@Resource
annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.As a specific consequence of this semantic difference, beans which are themselves defined as a collection or map type cannot be injected via
@Autowired
since type matching is not properly applicable to them. Use@Resource
for such beans, referring to the specific collection/map bean by unique name.
So use this in your test, and it works fine:
@Resource(name="myList") private List<String> stringList;