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()
posted @ 2022-07-19 15:40  卷饼侠  阅读(25)  评论(0)    收藏  举报