2017-10-19 72 views
1

我想使用Spring,并遇到麻烦使用它的情况。 我有以下代码:使用嵌套的bean时,依赖注入与Spring不工作

<util:map id="someMap" value-type="java.util.Set"> 
    <entry key="a" value-ref="setA"/> 
    <entry key="b" value-ref="setB"/> 
</util:map> 

<util:set id="setA"> 
    <value>A</value> 
</util:set> 

<util:set id="setB"> 
    <value>B</value> 
</util:set> 

与下面的Java代码(使用@Qualifier得到 “someMap”):

package a.b.c; 
public class SomeClass { 
    private final Map<String, Set<String>> someMap; 
    @Autowired 
    public SomeClass(@Qualifier("someMap") final Map<String, Set<String>> someMap) { 
     this.someMap = someMap; 
    } 
} 

和它给我下面的错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'a.b.c.SomeClass': Unsatisfied dependency expressed through constructor argument with index 2 of type [java.util.Map]: : No matching bean of type [java.util.Set] found for dependency [map with value type java.util.Set]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:{@org.springframework.beans.factory.annotation.Qualifier(value=someMap)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.util.Set] found for dependency [map with value type java.util.Set]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=someMap)}

看起来它很难找到Set,但我不知道它为什么会发生。你会如何解决这个问题?

+0

是否使用spring 4.3或更高版本version.lower春天版本,你不能autowire集合 – Assen

回答

0

我发现如何解决这个问题的尝试。我不得不使用@Value("#{@someMap}")而不是@Qualifier("someMap")

0

我希望你使用Spring 4.3或以上版本 与添加mapclass,键型

<util:map id="AdditionalParams" map-class="java.util.HashMap" 
      key-type="java.lang.String" value-type="java.lang.String"> 
+0

谢谢。这当然有助于我提出解决方案。 – yck