相关疑难解决方法(0)

使用util模式自动连接List会产生NoSuchBeanDefinitionException

我有一个bean,我想使用Spring util命名空间注入一个命名列表, <util:list id="myList">但Spring正在寻找一个类型为String的bean集合.我破碎的测试是:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ListInjectionTest {

    @Autowired @Qualifier("myList") private List<String> stringList;

    @Test public void testNotNull() {
        TestCase.assertNotNull("stringList not null", stringList);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的背景是:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

   <util:list id="myList">
       <value>foo</value>
       <value>bar</value>
   </util:list>

</beans>
Run Code Online (Sandbox Code Playgroud)

但我明白了

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [collection of 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), @org.springframework.beans.factory.annotation.Qualifier(value=myList)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:726) …
Run Code Online (Sandbox Code Playgroud)

java spring

89
推荐指数
1
解决办法
4万
查看次数

Spring无法自动装配Map bean

我在春天定义了一张地图:

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

    <entry key="Start" value="12345" />
    <entry key="Finish" value="12365" />
</util:map>
Run Code Online (Sandbox Code Playgroud)

然后我将这个bean自动装配到一个定义为:

private @Autowired @Qualifier(value = "AdditionalParams") Map<String, String> additionalParams;
Run Code Online (Sandbox Code Playgroud)

执行此操作时,会抛出一个异常,说:

引起:org.springframework.beans.factory.BeanCreationException:创建名为'DutyCreator'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private java.util.Map DutyCreator.additionalParams; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:[java.lang.String中]找到的依赖性[地图值java.lang.String类型]无类型的匹配豆:预期至少1豆,其有资格作为候选自动装配对于这种依赖.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[java.lang.String]的匹配bean为依赖[map with value type java.lang.String]:期望至少有一个bean有资格作为autowire候选者对于这种依赖.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}

有任何想法吗?

干杯.

java spring map

57
推荐指数
1
解决办法
4万
查看次数

标签 统计

java ×2

spring ×2

map ×1