多实例bean生命周期
(1)获取bean时创建
(2)通过构造器创建bean实例
(1)为bean的属性设置值或对其他bean引用(调用set方法)
(2)调用bean的初始化的方法
(3)bean可以使用了
(4)当容器关闭时候,容器不会调用bean的销毁方法
(1)创建一个对象
public class Book {
private String bname;
private Integer id;
public Book() {
System.out.println("Book创建了");
}
public void init()
{
System.out.println("Book初始化了");
}
public void destroy()
{
System.out.println("Book销毁了");
}
public void setBname(String bname) {
this.bname = bname;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", id=" + id +
'}';
}
}
(2)配置文件
<bean id="book" class="com.orz.spring.bean.Book" init-method="init" destroy-method="destroy" scope="prototype">
<property name="id" value="1"/>
<property name="bname" value="急急急"/>
</bean>
(3)测试
public void test1() {
ConfigurableApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
System.out.println("IOC容器加载完成");
Book book = applicationContext.getBean("book", Book.class);
System.out.println(book);
applicationContext.close();
}
(4)结果
IOC容器加载完成
Book创建了
Book初始化了
Book{bname='急急急', id=1}