Reputation: 11
I have written code that uses spring util namespace. I have a class named City with the following properties:
private List<String> name;
private List<String> state;
private List<Integer> population;
//setter and getter methods go here ...
and I configured the application context as:
<util:list id="cities" list-class="java.util.ArrayList">
p:name="chennai" p:state="tamilnadu" p:population="2000000"/>
<bean class="com.example2.City"
p:name="bang" p:state="karnataka" p:population="3000000"/>
</util:list>
When I run the application it gives me the following error:
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.example2.City
Will someone help me out? Thanks.
Upvotes: 1
Views: 890
Reputation: 62864
I think you have a wrong City
class defined. Why does a City have to have a list of names and a list of population numbers ?
I think City
should look more like:
public class City {
private String name;
private String state;
private String population;
//accessors
}
In this case, the <util:list>
should look like:
<util:list id="cities" list-class="java.util.ArrayList">
<bean class="com.example2.City"
p:name="bang"
p:state="karnataka"
p:population="3000000"/>
<bean class="com.example2.City"
p:name="chennai"
p:state="tamilnadu"
p:population="2000000"/>
</util:list>
and you will have a list of two cities.
Upvotes: 1