1、创建Student.java
package org.com.bean
public class Student(){
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
2、applicatioContext.xml注入Student类
<--id:代表实例化名字 class:代表实例化的类-->
<bean id="student" class="org.com.bean.Student" scope="prototype">
<property name="name" value="Jack"></property>
</bean>
<bean id="student1" class="org.com.bean.Student1" scope="prototype">
<property name="name" ref="student"></property>
</bean>
<-- ref:对类引用静态赋值 -->
3、ApplicationContext对象获得类的实例
public static void main(String [] args){
//启动Spring,装载上下文
ApplicationContext ac = new ClassPathXmlApplicationContext("applicatioContext.xml");
// 获得配置的Student实例
Student student = (Student) ac.getBean("student");
System.out.println(student);
System.out.println(student.getName());
Student1 student1= (Student1) ac.getBean("student1");
System.out.println(student1.getName());
}