package exm.spring.bean;public class User { private String name; private String password; public User() { super(); // TODO Auto-generated constructor stub } public User(String name, String password) { super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; }}package exm.spring.bean;public class Car { private String name; private String color; public Car() { super(); // TODO Auto-generated constructor stub } public Car(String name, String color) { super(); this.name = name; this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; }}package exm.spring.bean;public class Student { private String name; private String id; private String address; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, String id, String address) { super(); this.name = name; this.id = id; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student [name=" + name + ", id=" + id + ", address=" + address + "]"; }}package exm.spring.bean;public class Faculty { private String name; private String id; private String address; public Faculty() { super(); // TODO Auto-generated constructor stub } public Faculty(String name, String id, String address) { super(); this.name = name; this.id = id; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Faculty [name=" + name + ", id=" + id + ", address=" + address + "]"; }}package exm.spring.bean;public class StaticFactory { private User user; public void setUser(User user) { this.user = user; } public User getUser() { return this.user; } public static User getBean() { return new User("李佳佳", "12345678"); }}package exm.spring.bean;public class InstanceFactory { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public User getBean() { return user; }}package exm.spring.bean;import org.springframework.beans.factory.FactoryBean;/** * 实现了FactoryBean的类配置在容器中, * 但是容器getBean()获取的是该类getObject()返回的类 * @author Administrator * */public class CarFactory implements FactoryBean{ private Car car; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public Car getObject() throws Exception { return car; } @Override public Class getObjectType() { return Car.class; } @Override public boolean isSingleton() { return true; }}//测试类package exm.spring.bean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { @org.junit.Test public void test() { /** * 下面是测试实例化bean的三种方式 */ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); User user1 = (User) ac.getBean("user"); System.out.println(user1.toString()); User user2 = (User) ac.getBean("user2"); System.out.println(user2.toString()); User user3 = (User) ac.getBean("user3"); System.out.println(user3.toString()); Car car = (Car) ac.getBean("car"); System.out.println(car); /** * 下面是测试父bean和子bean */ Student student = ac.getBean("student", Student.class); Faculty faculty = ac.getBean("faculty", Faculty.class); System.out.println(student.toString()); System.out.println(faculty.toString()); //销毁容器 ((ClassPathXmlApplicationContext)ac).close(); }}