java - Bean collection Spring: ConversionNotSupportedException -
i'm new in spring , i'm try create simple quick start application. application composed 3 java classes, pom.xml file , xml file:
produttore.java:
package com.mkyong.core; import org.springframework.stereotype.component; @component public class produttore { private int id; private string ruolo; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getruolo() { return ruolo; } public void setruolo(string ruolo) { this.ruolo = ruolo; } @override public string tostring(){ return "produttore " + id + ", ruolo " + ruolo; } }
team.java:
package com.mkyong.core; import java.util.list; import org.springframework.stereotype.component; @component public class team { private produttore leader; private list<produttore> membri; public produttore getleader() { return leader; } public void setleader(produttore leader) { leader = leader; } public list<produttore> getmembri() { return membri; } public void setmembri(list<produttore> membri) { this.membri = membri; } public void addmembri(produttore p){ this.membri.add(p); } @override public string tostring(){ return "trainer " + leader + " numero membri: " + membri.size(); } }
test1.java:
package com.mkyong.core; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class test1 { public static void main(string[] args){ applicationcontext context = new classpathxmlapplicationcontext(new string[]{"test1xml.xml"}); team ris=(team)context.getbean("teambean"); system.out.println(ris); } }
test1xml.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="produttorebean" class="com.mkyong.core.produttore"> <property name="id" value="1234"/> <property name="ruolo" value="trainer"/> </bean> <bean id="teambean" class="com.mkyong.core.team"> <property name="leader"> <bean class="com.mkyong.core.produttore"> <property name="id" value="1111"></property> <property name="ruolo" value="trainer"/> </bean> </property> <property name="membri"> <util:list list-class="java.util.arraylist"> <value>0</value> <ref bean="produttorebean2" /> </util:list> </property> </bean> <bean id="produttorebean2" class="com.mkyong.core.produttore"> <property name="id" value="1112" /> <property name="ruolo" value="junior"/> </bean> </beans>
pom.xml:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.mkyong.core</groupid> <artifactid>spring3example</artifactid> <packaging>jar</packaging> <version>1.0-snapshot</version> <name>spring3example</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.0.5.release</spring.version> </properties> <dependencies> <!-- spring 3 dependencies --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> </dependencies> </project>
i don't know why, when try lunch application fails. post error:
exception in thread "main" org.springframework.beans.factory.beancreationexception: error creating bean name 'teambean' defined in class path resource [test1xml.xml]: initialization of bean failed; nested exception org.springframework.beans.conversionnotsupportedexception: failed convert property value of type 'java.util.arraylist' required type 'java.util.list' property 'membri'; nested exception java.lang.illegalstateexception: cannot convert value of type [java.lang.string] required type [com.mkyong.core.produttore] property 'membri[0]': no matching editors or conversion strategy found @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:527) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:288) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:190) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:580) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:895) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:425) @ org.springframework.context.support.classpathxmlapplicationcontext.<init>(classpathxmlapplicationcontext.java:139) @ org.springframework.context.support.classpathxmlapplicationcontext.<init>(classpathxmlapplicationcontext.java:93) @ com.mkyong.core.test1.main(test1.java:8) caused by: org.springframework.beans.conversionnotsupportedexception: failed convert property value of type 'java.util.arraylist' required type 'java.util.list' property 'membri'; nested exception java.lang.illegalstateexception: cannot convert value of type [java.lang.string] required type [com.mkyong.core.produttore] property 'membri[0]': no matching editors or conversion strategy found @ org.springframework.beans.beanwrapperimpl.convertifnecessary(beanwrapperimpl.java:462) @ org.springframework.beans.beanwrapperimpl.convertforproperty(beanwrapperimpl.java:499) @ org.springframework.beans.beanwrapperimpl.convertforproperty(beanwrapperimpl.java:493) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.convertforproperty(abstractautowirecapablebeanfactory.java:1371) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applypropertyvalues(abstractautowirecapablebeanfactory.java:1330) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1086) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:517) ... 11 more caused by: java.lang.illegalstateexception: cannot convert value of type [java.lang.string] required type [com.mkyong.core.produttore] property 'membri[0]': no matching editors or conversion strategy found @ org.springframework.beans.typeconverterdelegate.convertifnecessary(typeconverterdelegate.java:231) @ org.springframework.beans.typeconverterdelegate.converttotypedcollection(typeconverterdelegate.java:520) @ org.springframework.beans.typeconverterdelegate.convertifnecessary(typeconverterdelegate.java:173) @ org.springframework.beans.beanwrapperimpl.convertifnecessary(beanwrapperimpl.java:447) ... 17 more
could me? lot
convert value of type [java.lang.string] required type [com.mkyong.core.produttore] property 'membri[0]' : tells spring unable convert string value @ first position in arraylist type produttore. see explanation below :-
private list<produttore> membri;
the error because of above line. because here defining arraylist name membri contain objects of type produttore putting string value @ first position in in xml i.e. 0:
<util:list list-class="java.util.arraylist" value- type="com.mkyong.core.produttore"> *<value>0</value>* <ref bean="produttorebean2" /> </util:list>
if convert decalaration of membri in team class below code work:
private list<object> membri; public list<object> getmembri() { return membri; } public void setmembri(list<object> membri) { this.membri = membri; }
Comments
Post a Comment