A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Frameworks
»
Spring
Author
Spring with Generics Collection
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
posted
Jan 24, 2013 03:16:54
0
Hi All,
I have written a small program for list using spring framework.This is what i have written
Context file
<?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.0.xsd"> <bean id="collectionspring" class="org.springtest.SpringCollection"> <property name="address1"> <list> <ref bean="address12" /> <value>dehradun</value> <value>Uttarakhand</value> </list> </property> </bean> <bean id="address12" class ="org.springtest.Address"> <property name="address1" value="Bangalore" /> </bean> </beans>
two files are there for address setting
package org.springtest; public class Address { private String address1; public String getAddress1() { return address1; } public void setAddress1(String address1) { this.address1 = address1; } }
package org.springtest; import java.util.List; public class SpringCollection { List<Address> address1; public List<Address> getAddress1() { return address1; } public void setAddress1(List<Address> address1) { this.address1 = address1; } }
and my MAIN CLASS..
package org.springtest; import java.util.Iterator; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringCollectionTest { public static void main(String[] args) { List i; ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml"); SpringCollection sc=(SpringCollection)context.getBean("collectionspring"); System.out.println(sc.getAddress1()); } }
However its throwing an error
Can anyone help me in this regard
Mark Spritzler
ranger
Sheriff
Joined: Feb 05, 2001
Posts: 17234
1
I like...
posted
Jan 24, 2013 10:02:23
0
Yes, you are trying to add two Strings to a Collection that only takes Address objects. This would throw an exception in Java without Spring.
<value>someString</value>
Why would you expect otherwise?
Mark
Perfect World Programming, LLC
-
Two Laptop Bag
-
Tube Organizer
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Spring with Generics Collection
Similar Threads
Junit + ContextConfiguration
@Autowired problem
Spring,JUnit, Hibernate and persistence db but not insert
tell me what is this error?
getting same result in AutoWiring byType and byName
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter