Results 1 to 5 of 5

Thread: Problems using util:map

  1. #1
    Join Date
    May 2008
    Location
    Berlin, Germany
    Posts
    36

    Default Problems using util:map

    Using spring 2.5.5. Configuration runs annotated driven. Component Scan is activated.
    A number of annotated classes working properly. Proper namespaces are added to spring configuration.

    Problem. Spring container does not properly autowire my util:map bean

    Excerpt from the bean-config:
    <util:map id="networkTransportMap" >
    <entry key="abc" value="def"/>
    </util:map>


    Excerpt from the @Component annotated class using the map:
    @Autowired
    @Qualifier("networkTransportMap")
    private Map<String,String> networkTransportMap;

    Log excerpt:
    Error creating bean with name 'Bar': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private java.util.Map foo.Bar.networkTransportMap; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: 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.Aut owired(required=true), @org.springframework.beans.factory.annotation.Qual ifier(value=networkTransportMap)}
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessAfterIn stantiation(AutowiredAnnotationBeanPostProcessor.j ava:243)


    What am I doing wrong ??

  2. #2
    Join Date
    Aug 2008
    Location
    London
    Posts
    40

    Default

    Code:
    @Autowired
    @Qualifier("networkTransportMap")
    private Map<String,String> networkTransportMap;
    In this case, Spring will actually try to autowire all beans of type java.lang.String, where the map key will be bean name. If you want to inject <util:map/> bean, use @Resource instead.

  3. #3
    Join Date
    Dec 2007
    Posts
    130

    Default

    The problem is with @Qualifier("networkTransportMap"). Qualifier is not the name of the bean, but a different way to classify beans for autowire. To mark a bean as being of a given qualifier, add <qualifier> in it's definition. For example:

    Code:
    <util:map id="networkTransportMap" >
    <entry key="abc" value="def"/>
    <qualifier value="networkTransportMap"/>
    </util:map>
    NOTE: I suppose that util:map accepts qualifiers. Haven't tested it myself

  4. #4
    Join Date
    Dec 2008
    Posts
    1

    Default

    كسمككككككككككككككككككككككككككككككككككككككككككككككك كككككك

  5. #5

    Default

    @Autowired is based on types. I believe if you simply use @Resource(name="foo") it will work just fine.

Posting Permissions

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