Results 1 to 3 of 3

Thread: util:map key-type="java.lang.Class"

  1. #1
    Join Date
    Nov 2006
    Posts
    16

    Default util:map key-type="java.lang.Class"

    Hi experts,
    Is there a working example-xml-snippet wich shows correct usage of a Map<java.util.Class,xxx> bean-definition?

    Code:
    <util:map id="myMap" key-type="java.lang.Class" value-type="java.lang.String">
    	<entry key="java.lang.String" value="Hallo"></entry>
    </util:map>
    should be mapped to
    Code:
    @Autowired
    @Qualifier("myMap")
    Map<java.util.Class,java.lang.String>
    I always get the Exception:

    Code:
     org.springframework.beans.FatalBeanException: Key type [class java.lang.Class] of map [java.util.Map] must be assignable to [java.lang.String]
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
    	... 27 more
    Caused by: org.springframework.beans.FatalBeanException: Key type [class java.lang.Class] of map [java.util.Map] must be assignable to [java.lang.String]
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:585)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
    	... 29 more
    I checked the spring-source-code at DefaultListableBeanFactory.resolveDependency(Defau ltListableBeanFactory.java:585).
    Code:
    Class keyType = descriptor.getMapKeyType();
    			if (keyType == null || !String.class.isAssignableFrom(keyType)) {
    but it seams that mapping a key-type of java.util.Class will never work since
    Code:
    String.class.isAssignableFrom( Class.class )
    evaluates to 'false'
    Last edited by mcbain; Aug 10th, 2009 at 01:46 PM.

  2. #2
    Join Date
    Nov 2006
    Posts
    16

    Default

    As a workaround the bean can be injected by 'name' instead.

    Code:
    @Resource(name="myMap")
    Map<...> myMap;

  3. #3
    Join Date
    Dec 2006
    Posts
    6

Posting Permissions

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