java - Issue while autowiring strongly typed collections -
i using spring 3.2.2. facing issue while autowring collection spring's bytype autowiring mode. have created below example.
bean defination:
package com.springaction.testmultidimesionalcollection; import java.util.arraylist; /** * @author jinesh * */ public class address { arraylist<country> country; public arraylist<country> getcountry() { return country; } /** * @param country country set */ public void setcountry(arraylist<country> country) { this.country = country; } } below spring configuration file testmultidimensionalcollection.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-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="addressmiddleeast" class="com.springaction.testmultidimesionalcollection.address" autowire="bytype"> </bean> <bean id="countrychina" class="com.springaction.testmultidimesionalcollection.country"> <property name="countryname" value="china" /> </bean> <bean id="countryindia" class="com.springaction.testmultidimesionalcollection.country"> <property name="countryname" value="india" /> </bean> <bean id="countryaus" class="com.springaction.testmultidimesionalcollection.country"> <property name="countryname" value="australia" /> </bean> <bean id="middeastcountryquatar" class="com.springaction.testmultidimesionalcollection.country"> <property name="countryname" value="quatar" /> </bean> <bean id="middeastcountryisrael" class="com.springaction.testmultidimesionalcollection.country"> <property name="countryname" value="israel" /> </bean> <bean id="middeastcountryyemen" class="com.springaction.testmultidimesionalcollection.country"> <property name="countryname" value="yemen" /> </bean> </beans> here have strong type collection in address class. elements of country supposed added inside country properties arraylist when executing below code getting null pointer exception.
package com.springaction.testmultidimesionalcollection; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; /** * @author jinesh * */ public class testmultidimensionalmain { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub applicationcontext context = new classpathxmlapplicationcontext("com/springaction/testmultidimesionalcollection/testmultidimensionalcollection.xml"); address addrs=(address)context.getbean("addressmiddleeast"); system.out.println("address size:" + addrs.getcountry().size()); } } i not able understand, why spring not able automatically detect country beans , add them array list property of address bean? have missed in configuration?
you haven't requested autowired. bean
<bean id="addressmiddleeast" class="com.springaction.testmultidimesionalcollection.address" autowire="bytype"> </bean> and class
public class address { arraylist<country> country; public arraylist<country> getcountry() { return country; } /** * @param country country set */ public void setcountry(arraylist<country> country) { this.country = country; } } simply has property setters/getters. need use @autowired (or related annotations).
public class address { @autowired arraylist<country> country; if can't use @autowired (why not???), need create bean of type list has reference each of country beans, in (now deleted) tichodrama's answer.
this explained in spring ioc documentation here
Comments
Post a Comment