In my @Configuration file, I have beans with a relationship similar to the following:
@Bean
public Cache<Foo,Bar> fooBarCache(Map<Foo,Future<Bar>> refreshTaskMap) {
return new AsyncUpdateCache(refreshTaskMap);
}
@Bean
public Map<Foo,Future<Bar>> refreshTaskMap() {
return new HashMap<Foo,Future<Bar>>();
}
However, the ApplicationContext fails to load because it complains about "No qualifying bean of type [com.example.Bar]". From what I can tell, Spring is trying to be cute with creating a map for me and assumes I intend to use the map to lookup beans, or something similar.
How do I prevent it from trying to do its collection-injection "magic" and just inject the bean as I've declared it? I've tried adding a @Qualifier annotation on the fooBarCache argument, but that didn't seem to help.
You can use another approach to bean dependency injection - invoking the bean factory method instead of taking it in as a parameter (Spring doc). Then your configuration would look like this:
Spring is smart enough to realize that you want to use the bean
refreshTaskMap
and not simply call the method and instead of creating a new and unmanaged map instance it will replace the call with a lookup of an existingrefreshTaskMap
bean. That is described further here.If you intend to autowire
refreshTaskMap
in other beans (outside of this configuration class), the Springs semantics of@Autowire Map<String, V>
is to autowire a map of all beans of the typeV
, where keys are the bean names (map key type must beString
) (reference)In that case, you should use
@Resource
: