bean管理--Scope、Init、Destroy
@Repository
@Scope("singleton") // 控制Bean范围
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("book dao constructor ...");
}
@PostConstruct // 构造方法后
public void init(){
System.out.println("book init ...");
}
@PreDestroy // 程序销毁前
public void destroy(){
System.out.println("book destory ...");
}
}
@PostConstruct 和 @PreDestroy 控制Bean生命周期
// 销毁操作没有运行,要关闭容器,使用AnnotationConfigApplicationContext
// ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
.....
.....
ctx.close()