我有一个用户细节类里面,我放置地址类来存储每个用户细节类的多地址。我正在使用Spring 4.0。下面给出的代码:在春天用豆列表
的UserDetails类:
@Component("userDetails")
public class UserDetails {
@Resource(name="address")
@Autowired
private List<Address> address;
public List<Address> getAddress() {
return address;
}
@Autowired
public void setAddress(List<Address> address) {
this.address = address;
}
}
地址类:
@Component("address")
public class Address {
private String area;
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
在这个例子中,Address.area值需要在运行时通过,然后我需要创建地址类的对象。然后它需要添加UserDetails类中的List地址变量。同样,我需要在arrayList中添加n个数字对象,然后我需要为UserDetails类创建一个对象。
我尝试下面的代码:
public class AppMain {
public static void main(String args[]){
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Address address = (Address)context.getBean("address");
//setting first value:
address.setArea("XXX");
Address address1 = (Address)context.getBean("address");
//setting second value
address1.setArea("YYY");
UserDetails userDetails = (UserDetails)context.getBean("userDetails");
System.out.println("User Size: "+application.getAddress().size());
System.out.println("User Details : "+application.getAddress().get(0).getArea());
System.out.println("User Details : "+application.getAddress().get(1).getArea()); // getting ArrayIndexOutOfBoundException in this line
}
}
部分输出: 用户大小:1 用户详情:YYY
预期输出: 用户大小:2 用户详情:XXX 用户详情:YYY
您能否帮忙解决这个问题。
在运行时只有我来知道有多少地址块将要创建。 – Praveen