(1)数据结构
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private long id; private String name; private int age; private List<Address> addresses; }
(2)xml配置
<bean id="student" class="com.yk.entity.Student"> <property name="id" value="100001"></property> <property name="name" value="闫凯"></property> <property name="age" value="27"></property> <property name="addresses"> <list > <ref bean="address"></ref> <ref bean="address1"></ref> </list> </property> </bean> <bean id="address" class="com.yk.entity.Address"> <property name="name" value="建设一路"></property> <property name="id" value="10001"></property> </bean> <bean id="address1" class="com.yk.entity.Address"> <property name="name" value="建设二路"></property> <property name="id" value="10002"></property> </bean>
(3)测试
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); Student student=(Student) applicationContext.getBean("student"); System.out.println(student);