我是春天的新手。我得到了这个例外。我用谷歌搜索但我没有找到确切的解决方案,任何人都可以指出哪里是错误..
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean class="com.listinjection.Course" id="course">
<property name="course">
<list value-type="java.lang.String">
<value>c</value>
<value>c++</value>
<value>java</value>
<value>web services</value>
</list>
</property>
</bean>
<bean class="com.mapinjection.University" id="university">
<property name="university">
<map key-type="java.lang.String" value-type="com.mapinjection.Course">
<entry key="java" value-ref="course">
</entry>
</map>
</property>
</bean>
</beans>
package com.mapinjection;
import java.util.List;
public class Course {
private List<String> course;
public void setCourse(List<String> course) {
this.course = course;
}
public void display(){
System.out.println("list of courses are:");
for(String s:course){
System.out.println(s);
}
}
}
package com.mapinjection;
import java.util.Map;
public class University {
private Map<String, Course> university;
public void setUniversity(Map<String, Course> university) {
this.university = university;
}
public void display(){
Course course1=university.get("java");
course1.display();
}
}
package com.mapinjection;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"com/mapinjection/app-context.xml");
University university=context.getBean("university", University.class);
university.display();
context.close();
}
}
Jul 25, 2014 10:09:15 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15d2aa14: startup date [Fri Jul 25 10:09:15 IST 2014]; root of context hierarchy
Jul 25, 2014 10:09:15 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/mapinjection/app-context.xml]
Jul 25, 2014 10:09:15 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@614951ff: defining beans [course,university]; root of factory hierarchy
Jul 25, 2014 10:09:15 AM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@614951ff: defining beans [course,university]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'university' defined in class path resource [com/mapinjection/app-context.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 'university'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.listinjection.Course] to required type [com.mapinjection.Course] for property 'university[java]': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.mapinjection.Test.main(Test.java:9)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'university'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.listinjection.Course] to required type [com.mapinjection.Course] for property 'university[java]': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:463)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:494)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:488)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1464)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1423)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.listinjection.Course] to required type [com.mapinjection.Course] for property 'university[java]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:648)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:208)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
... 17 more
在整个代码中我没有使用LinkedHashMap,但为什么它说“无法将类型'java.util.LinkedHashMap'的属性值转换为属性'university'的必需类型'java.util.Map'”
答案 0 :(得分:8)
您正尝试将com.listinjection.Course
类型的值添加到期望Map
值的com.mapinjection.Course
。
<map key-type="java.lang.String" value-type="com.mapinjection.Course">
<entry key="java" value-ref="course">
纠正这一点。