异常如下
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
代码如下:
@Autowired
private Map<String, String> testMap;
配置如下:
<util:map id="testMap">
<entry key="101" value="foo" />
</util:map>
原因如下:
注入Map类型的bean时,由于@Autowired按类型注入的,找不到对应Key中String类型的Bean,所有报错,用@Resource替代即可。
解决:
@Resource
private Map<String, String> testMap;