Stu.java
package com.yan;
import java.util.List;
public class Stu {
private String username;
private Integer xuehao;
private List<String> list;
public Stu(String username,Integer xuehao,List<String> list) {
super();
this.username = username;
this.xuehao = xuehao;
this.list = list;
}
public Stu() {
super();
}
public void setUsername(String username) {
this.username = username;
}
public void setXuehao(Integer xuehao) {
this.xuehao = xuehao;
}
public void setList(List<String> list) {
this.list = list;
}
public String toString() {
return "User {学生=" + username + ", 学号=" + xuehao + ", list=" + list + "}";
}
}
GraStu.java
package com.yan;
import com.yan.Stu;
public class GraStu {
private String teacher;
private Stu m;
private Stu n;
public void setStu1(Stu n){
this.n = n;
}
public void setStu(Stu m){
this.m = m;
}
public String teacher() {
return "teacher";
}
public GraStu(String teacher) {
super();
this.teacher = teacher;
}
public GraStu() {
super();
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public void setN(Stu n) { this.n = n;}
public void setM(Stu m) { this.m = m;}
public String toString1() {
return "User {" + n + " 老师:" + teacher + "}";
}
public String toString() {
return "User {" + m + " 老师:" + teacher + "}";
}
}
abc.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 将指定类配置给Spring,让Spring创建其对象的实例 -->
<bean id="stu1" class="com.yan.Stu" >
<constructor-arg index="0" value="耀哥" />
<constructor-arg index="1" value="133233" />
<constructor-arg index="2">
<list>
<value>"constructorvalue1"</value>
<value>"constructorvalue2"</value>
</list>
</constructor-arg>
</bean>
<bean id="stu2" class="com.yan.Stu">
<property name="username" value="小耀耀"></property>
<property name="xuehao" value="11111111"></property>
<property name="list">
<list>
<value>"setlistvalue1"</value>
<value>"setlistvalue2"</value>
</list>
</property>
</bean>
<bean id="userGrad" class="com.yan.GraStu">
<property name="m" ref="stu1" />
<property name="teacher" value="小明"></property>
</bean>
<bean id="useGrad" class="com.yan.GraStu">
<property name="m" ref="stu2" />
<property name="teacher" value="小王"></property>
</bean>
</beans>
Text.java
package com.yan;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
@SuppressWarnings("unused")
public class Text {
public static void main(String[] args) {
String xmlPath = "com/yan/abc.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext (xmlPath);
System.out.println(applicationContext.getBean("stu1"));
System.out.println(applicationContext.getBean("stu2"));
System.out.println(applicationContext.getBean("userGrad"));
System.out.println(applicationContext.getBean("useGrad"));
}
}