spring - 实用程序 :map in Spring picking up every bean of the map type

标签 spring map

我遇到了一个让我流泪的 Spring map 问题。

我的 Spring 是这样的:

<?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>

然后我 Autowiring 的代码如下(不相关部分省略):

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

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

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

[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

我还不能让条目的键部分实际显示为键。如果我将 map 改回 Map 并将一些添加到我的 spring 中,那么 map 会填充这些,使用它们的 id 作为键。

我在这里很困惑,过去使用过相当数量的 Spring 。如果有人知道这里到底发生了什么,我将不胜感激。

提前致谢。

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

但解决方案是使用@Resource,我已经在这样做了..

最佳答案

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

关于spring - 实用程序 :map in Spring picking up every bean of the map type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12324082/

相关文章:

java - org.apache.catalina.LifecycleException : Failed to start component [StandardEngine[Catalina]. StandardHost[本地主机].StandardContext[/mmasgis]]

java - 如何使用 spring 框架让服务器通知客户端有关更改的信息?

android - Google Android map 上的不同命名标记

java - Spock-Spring - 如何控制围绕数据驱动测试的事务何时被回滚?

Spring Boot 安全性 - 多个身份验证提供程序

Spring:在启动时配置应初始化哪些服务 bean 的最佳方法是什么?

c++ - 广泛使用OpenGL绘制世界地图[WGS84]

ios - 缩小和放大后 MapOverlay 闪烁-iOS

c++ - std::map 运算符 [] 和 size() 的排序

java - Android 上的平铺 map