通过注解懒加载:
在Bean上加上@Lazy
在@Configuration下加入@Lazy,则所有Bean都开启懒加载模式
通过注解编写Bean初始化及销毁:
1.将Bean的类实现DisposableBean接口 与 InitializingBean接口
在destroy()方法 //销毁
与afterPropertiesSet()方法//初始化
下进行操作
2.在自己写的销毁或者初始化方法上加上
@PostConstruct //初始化
@PreDestroy //销毁
3.在@Bean中加initMethod,destroyMethod :
@Bean(initMethod = "",destroyMethod = "")
实例:
TestBean.Class
TestBean.class
public void onInit() {
System.out.println("TestBean.onInit");
}
public void onDestory() {
System.out.println("TestBean.onDestory");
}
Configuration.class
@Bean(initMethod = "onInit1",destroyMethod = "onDestory1")
public TestBean testBean() {
return new TestBean();
}