util:map 在 Spring 中获取 map 类型的每个 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12324082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:21:59  来源:igfitidea点击:

util:map in Spring picking up every bean of the map type

springmap

提问by Craig

I have a Spring map problem that is bringing me to tears.

我有一个让我流泪的 Spring 地图问题。

My spring looks like this:

我的春天是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    ">

   <util:map id="mockMap">
        <entry key="userTest1" value="test1"/>
        <entry key="userTest2" value="test2"/>
        <entry key="userTest3" value="test6"/>
    </util:map>
</beans>

Then the code where I am autowiring this is as follows (irrelevant parts ommitted):

然后我自动装配的代码如下(省略了不相关的部分):

@Autowired
@Resource(name="mockMap")
Map<String, String> testMap;

@Test
public void testGetGearListActivityOK() {
    for (String key : testMap.keySet()) {
        System.out.println("key = " + key);
    }
}

Amazingly enough, this will actually give me an error on the autowiring step saying that there are no beans matching type String. However, if I change the map in the unit-test to be defined as Map then I get the follow output:

令人惊讶的是,这实际上会在自动装配步骤中给我一个错误,说没有与 String 类型匹配的 bean。但是,如果我将单元测试中的地图更改为地图,那么我会得到以下输出:

[junit] key = mockMap
[junit] key = org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalRequiredAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalCommonAnnotationProcessor
[junit] key = systemProperties
[junit] key = systemEnvironment
[junit] key = messageSource
[junit] key = applicationEventMulticaster
[junit] key = lifecycleProcessor
[junit] key = mockMap
[junit] key = org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalRequiredAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalCommonAnnotationProcessor
[junit] key = systemProperties
[junit] key = systemEnvironment
[junit] key = messageSource
[junit] key = applicationEventMulticaster
[junit] key = lifecycleProcessor

I have not yet been able to get the key section of the entry to actually show up as a key. If I change the map back to Map and add some into my spring then the map populates with those, using their id's as the keys.

我还没有能够让条目的关键部分实际显示为一个关键。如果我将地图改回 Map 并将一些添加到我的弹簧中,那么地图就会填充这些,使用它们的 id 作为键。

I'm so confused here and have used spring a decent amount in the past. If anyone has a clue what the heck is going on here I'd be very grateful.

我在这里很困惑,过去曾大量使用过弹簧。如果有人知道这里发生了什么,我将不胜感激。

Thanks in advance.

提前致谢。

Also, I saw this question: Auto-wiring a List using util schema gives NoSuchBeanDefinitionException

另外,我看到了这个问题:Auto-wiring a List using util schema给出了NoSuchBeanDefinitionException

But the solution there is to use @Resource, which I am already doing..

但是那里的解决方案是使用@Resource,我已经在做..

回答by Ryan

Remove @Autowired and only use the @Resource(name="mockMap")

删除@Autowired,只使用@Resource(name="mockMap")

回答by Zigri2612

you can't autowire a collection like that.

你不能像这样自动装配一个集合。

However, you can try these:

但是,您可以尝试以下方法:

@Resource(name="mockMap")
private Map<String, String> testMap;

or even:

甚至:

@Value("#{mockMap}")
private Map<String, String> testMap;

Check the spring docs, the tips section:

检查spring 文档,提示部分:

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

本身定义为集合或映射类型的 bean 不能通过 @Autowired 注入,因为类型匹配不适用于它们。将 @Resource 用于此类 bean

回答by xiaohuo

only use @Resource and change "testMap" to "mockMap" in your config

只使用@Resource 并在你的配置中将“testMap”更改为“mockMap”