Closed vikuwo closed 1 month ago
It will be fine to use @Resource
, @Autowired
will find candidate by type.
Map, Collection(include Set and List) and Array are different when autowiring. They will find bean which has the same type as the element they storage.
But this shouldn't be the case when we use @Autowire
along with a @Qualifier
. It should autowire the exact bean. That way, if I have the multiple Maps or Collections of the same element type, I would still be able to autowire a specific bean.
It depends on how to understand the annotation Qualifier
. I think that it just provides a candidate, but not change the autowire logic. Maybe I am wrong.
At least the below statements should produce the same result. Otherwise the behaviour is not consistent.
System.out.printf("Autowired Bean: %s%n", qualifiedMap);
System.out.printf("Context Bean: %s%n", applicationContext.getBean("qualifiedMap", Map.class));
As far as I can tell, this works as of 6.2 due to #28122 - as a welcome side effect of that algorithmic optimization. Feel free to try the recently released 6.2.0-RC1 to double-check.
Affects: 6.1.0
Code:
Output:
When we mark a Map<String, Object> as @Bean with a qualifier, @Autowired refers to a different map. The bean seems fine when directly fetched from the application context.