spring 配置Map属性时的小问题
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newPerson' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'cars'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'test.collection.Car' for property 'cars[aa]': no matching editors or conversion strategy found Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newPerson' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'cars'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'test.collection.Car' for property 'cars[aa]': no matching editors or conversion strategy found at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at test.collection.main.main(main.java:11) Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'cars'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'test.collection.Car' for property 'cars[aa]': no matching editors or conversion strategy found at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:605) at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:617) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1577) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1536) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ... 11 more Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'test.collection.Car' for property 'cars[aa]': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306) at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:659) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:226) at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590) ... 17 more 以上错误信息来自XML得Map配置 代码如下: 修改正确后得代码如下: 错误原因是因为map 得entry 里边得value 是引用别得bean 而非自己创建,所以 value 应该改为value-ref.