I am new to spring.I got this exception.I googled but I did not find the exact solution for this can anyone point out where is the mistake..
<?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
In the entire code I have not used LinkedHashMap but why It is saying " Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'university'"
You are trying to add a value of type com.listinjection.Course
to a Map
that expects com.mapinjection.Course
values.
<map key-type="java.lang.String" value-type="com.mapinjection.Course">
<entry key="java" value-ref="course">
Correct that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With