Results 1 to 2 of 2

Thread: Autowiring XML defined <util:map does not work

  1. #1

    Question Autowiring XML defined <util:map does not work

    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:

    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>
    Then, I wrote a test case to autowire this map on my test class like this:

    Code:
    @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);
    
        }
    
    }
    When running the test class, I get the error

    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)}
    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?

    Forgot to say that I'm using spring 3.0.5.RELEASE

    Thanks,

    Francisco
    Last edited by francisco.ripoli; Mar 9th, 2012 at 04:06 AM. Reason: added spring version

  2. #2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •