Spring 的依赖注入在使用嵌套 bean 时不起作用
Dependency injection with Spring not working when using nested beans
我正在尝试使用 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)}
似乎找不到集合,但我不知道为什么会这样。你会如何解决这个问题?
希望您使用的是spring4.3以上版本
尝试添加 mapclass,key-type
<util:map id="AdditionalParams" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="java.lang.String">
我找到了解决这个问题的方法。我不得不使用 @Value("#{@someMap}")
而不是 @Qualifier("someMap")
。
我正在尝试使用 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)}
似乎找不到集合,但我不知道为什么会这样。你会如何解决这个问题?
希望您使用的是spring4.3以上版本 尝试添加 mapclass,key-type
<util:map id="AdditionalParams" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="java.lang.String">
我找到了解决这个问题的方法。我不得不使用 @Value("#{@someMap}")
而不是 @Qualifier("someMap")
。