[英]Issue while autowiring strongly typed collections
我正在使用spring 3.2.2。 我在通过spring的byType自动装配模式自动整理集合时遇到问题。 我创建了以下示例。
豆的定义:
package com.springaction.testmultidimesionalcollection;
import java.util.ArrayList;
/**
* @author jinesh
*
*/
public class Address {
ArrayList<Country> country;
public ArrayList<Country> getCountry() {
return country;
}
/**
* @param country the country to set
*/
public void setCountry(ArrayList<Country> country) {
this.country = country;
}
}
下面是弹簧配置文件testmultiDimensioncollection.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>
在这里,我在Address类中具有强类型集合。 因此,应该将国家的所有元素都添加到国家属性arraylist中,但是当我执行以下代码时,我得到了Null指针异常。
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());
}
}
我无法理解,为什么spring不能自动检测到国家Bean并将其添加到地址Bean的数组列表属性中? 我是否错过了配置中的某些内容?
您实际上并没有要求自动接线。 这个豆
<bean id="addressMiddleEast" class="com.springaction.testmultidimesionalcollection.Address" autowire="byType">
</bean>
和课
public class Address {
ArrayList<Country> country;
public ArrayList<Country> getCountry() {
return country;
}
/**
* @param country the country to set
*/
public void setCountry(ArrayList<Country> country) {
this.country = country;
}
}
仅具有属性设置器/获取器。 您需要使用@Autowired
(或相关的注释)。
public class Address {
@Autowired
ArrayList<Country> country;
如果您不能使用@Autowired
(为什么不使用?),则需要创建一个List
类型的Bean,该Bean引用了您的每个Country
Bean,例如tichodrama的答案(现已删除)。
此处在Spring IoC文档中对此进行了解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.