通过弹簧注入空图 [英] Inject empty map via spring

查看:79
本文介绍了通过弹簧注入空图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的@Configuration文件中,我具有与以下关系相似的bean:

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>>();
}

但是,ApplicationContext无法加载,因为它抱怨没有合格的bean输入[com.example.Bar]。据我所知,Spring试图为我创建地图很可爱,并假设我打算使用该地图查找bean或类似的东西。

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.

如何我要阻止它尝试执行其收集注入魔术,而只是按照我声明的那样注入bean?我尝试在fooBarCache参数上添加@Qualifier批注,但这似乎无济于事。

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.

推荐答案

使用另一种方法进行bean依赖注入-调用bean工厂方法而不是将其作为参数使用( Spring文档)。然后,您的配置将如下所示:

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:

@Bean
public Cache<Foo,Bar> fooBarCache() {
    return new AsyncUpdateCache(refreshTaskMap()); // call the method
}

@Bean
public Map<Foo,Future<Bar>> refreshTaskMap() {
    return new HashMap<Foo,Future<Bar>>();
}

Spring足够聪明,可以意识到您想使用 bean refreshTaskMap 而不是简单地调用该方法,而不是创建一个新的非托管地图实例,它将用现有的 refreshTaskMap bean。 此处

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 existing refreshTaskMap bean. That is described further here.

如果您打算在其他bean中自动装配 refreshTaskMap (此配置之外)类,则 @Autowire Map< String,V> 的Springs语义是自动装配所有 V ,其中键是Bean名称(映射键类型必须为 String )(参考

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 type V, where keys are the bean names (map key type must be String) (reference)


即使预期的键类型为String,即使键入的Maps也可以自动连线。 Map值将包含所有预期类型的​​bean,并且键将包含相应的bean名称

Even typed Maps can be autowired as long as the expected key type is String. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names

在这种情况下,您应使用 @资源

In that case, you should use @Resource:


本身定义为集合或映射的bean类型不能通过@Autowired注入,因为类型匹配不适用于它们。对此类bean使用@Resource,通过唯一名称引用特定的collection或map bean。

beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name.

这篇关于通过弹簧注入空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆