如一个类中包含另一个类的数组,LIST,SET,MAP 在Spring中配置会是怎样呢?例子如下
一个Stu类
public class Stu {
private String stuName;
private Integer stuAge;
public Integer getStuAge() {
return stuAge;
}
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
//一个USER类
public class User {
private String userName;
private Integer age;
private Stu[] stus1;
private List stus2;
private Set stus3;
private Map stus4;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Stu[] getStus1() {
return stus1;
}
public void setStus1(Stu[] stus1) {
this.stus1 = stus1;
}
public List getStus2() {
return stus2;
}
public void setStus2(List stus2) {
this.stus2 = stus2;
}
public Set getStus3() {
return stus3;
}
public void setStus3(Set stus3) {
this.stus3 = stus3;
}
public Map getStus4() {
return stus4;
}
public void setStus4(Map stus4) {
this.stus4 = stus4;
}
}
//spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="astu" class="org.nieweiguo.Stu" abstract="false"
singleton="true" lazy-init="default" autowire="constructor"
dependency-check="default">
<property name="stuName">
<value>阿猫</value>
</property>
<property name="stuAge">
<value>18</value>
</property>
</bean>
<bean id="astu2" class="org.nieweiguo.Stu" abstract="false"
singleton="true" lazy-init="default" autowire="constructor"
dependency-check="default">
<property name="stuName">
<value>阿狗</value>
</property>
<property name="stuAge">
<value>28</value>
</property>
</bean>
<bean id="user" class="org.nieweiguo.User" abstract="false"
singleton="true" lazy-init="default" autowire="constructor"
dependency-check="default">
<property name="userName">
<value>nieweiguo</value>
</property>
<property name="age">
<value>25</value>
</property>
<property name="stus1">
<list>
<ref bean="astu" />
<ref bean="astu" />
<ref bean="astu" />
</list>
</property>
<property name="stus2">
<list>
<ref bean="astu2" />
<ref bean="astu2" />
<ref bean="astu2" />
</list>
</property>
<property name="stus3">
<set>
<ref bean="astu" />
<ref bean="astu2" />
</set>
</property>
<property name="stus4">
<map>
<entry key="stu1">
<ref bean="astu" />
</entry>
<entry key="stu2">
<ref bean="astu2" />
</entry>
</map>
</property>
</bean>
</beans>
//TEST类
public class testSpring {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context =new FileSystemXmlApplicationContext("src/applicationContext.xml");
User user=(User)context.getBean("user");
//对象数组得到显示
Stu[] stus1=(Stu[])user.getStus1();
for(int i=0;i<stus1.length;i++)
{
System.out.println(stus1.getStuName());
}
//LIST得到显示
List stus2=user.getStus2();
for(int i=0;i<stus2.size();i++)
{
System.out.println(((Stu)stus2.get(i)).getStuName());
}
//Set 得到显示
Set stus3=user.getStus3();
Object stuso3[]=(Object[])stus3.toArray();
for(int i=0;i<stuso3.length;i++)
{
Stu a=(Stu)stuso3;
System.out.println(a.getStuName());
}
//Map得到显示
Map stus4=user.getStus4();
Stu a=(Stu)stus4.get("stu1");
Stu b=(Stu)stus4.get("stu2");
System.out.println(a.getStuName()+"aaaa");
System.out.println(a.getStuName()+"bbb");
}
}
输出结果如下:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
阿猫
阿猫
阿猫
阿狗
阿狗
阿狗
阿猫
阿狗
阿猫aaaa
阿猫bbb
还可以定义一个类的 初始化方法。
<bean id="user" class="org.nieweiguo.User" abstract="false"
singleton="true" lazy-init="default" autowire="constructor"
dependency-check="default" init-method="init">
</bean>
在类中 可以这样写
public void init()
{
this.userName="anie";
this.age=25;
}
Spring 初试 对象数组,LIST,SET
最新推荐文章于 2019-06-21 15:59:15 发布