还是举例说明:
QianDaye类
public class QianDaYe {
private String[] aiHaos;
//多个女性朋友
private Woman[] womens;
public void setAiHaos(String[] aiHaos) {
this.aiHaos = aiHaos;
}
public void setWomens(Woman[] womens) {
this.womens = womens;
}
@Override
public String toString() {
return "QianDaYe{" +
"aiHaos=" + Arrays.toString(aiHaos) +
", womens=" + Arrays.toString(womens) +
'}';
}
}
Woman类
public class Woman {
private String name;
@Override
public String toString() {
return "Woman{" +
"name='" + name + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
}
配置类spring-array.xml
<?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.xsd">
<bean id="w1" class="per.sxhzs.spring6.bean.Woman">
<property name="name" value="小花"/>
</bean>
<bean id="w2" class="per.sxhzs.spring6.bean.Woman">
<property name="name" value="小亮"/>
</bean>
<bean id="w3" class="per.sxhzs.spring6.bean.Woman">
<property name="name" value="小明"/>
</bean>
<bean id="yuQian" class="per.sxhzs.spring6.bean.QianDaYe">
<!--这个数组属性当中的元素类型是String简单类型-->
<property name="aiHaos">
<array>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</array>
</property>
<!--这个数组当中的类型就不是简单类型了。-->
<property name="womens">
<array>
<ref bean="w1"/>
<ref bean="w2"/>
<ref bean="w3"/>
</array>
</property>
</bean>
</beans>
测试类
@Test
public void testArray() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-array.xml");
QianDaYe yuQian = applicationContext.getBean("yuQian", QianDaYe.class);
System.out.println(yuQian);
}
标签:set,name,aiHaos,spring,Woman,数组,womens,public,String
From: https://www.cnblogs.com/sxhxh/p/16885603.html