Hi all!
I am defining a Map of Strings on a xml context file and trying to autowire this map into my test class. Something like this:
Then, I wrote a test case to autowire this map on my test class like this:Code:<?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-2.0.xsd "> <util:map id="myMap" key-type="java.lang.String" value-type="java.lang.String"> <entry key="key One" value="value One"/> <entry key="key Two" value="value Two"/> </util:map> </beans>
When running the test class, I get the errorCode:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations ="classpath:/test-autowire.xml") public class AutowireTest { @Autowired @Qualifier("myMap") Map<String, String> myMap; @Test public void test() { Assert.assertNotNull(myMap); } }
After some debugging, I found that at DefaultListableBeanFactory, when it calls findAutowireCandidates(..), is trying to find elements of type java.lang.String instead of a java.util.Map ... shouldn't that be the case?Code:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AutowireTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.util.Map AutowireTest.myMap; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching 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), @org.springframework.beans.factory.annotation.Qualifier(value=myMap)} ... Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching 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), @org.springframework.beans.factory.annotation.Qualifier(value=myMap)}
Forgot to say that I'm using spring 3.0.5.RELEASE
Thanks,
Francisco


Reply With Quote