转:https://blog.csdn.net/lizhiqiang1217/article/details/89950199
1、MainConfigOfLifeCycle
/**
* 2)、通过让Bean实现InitializingBean(定义初始化逻辑);
* 实现DisposableBean(定义销毁逻辑);
*/
@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
}
2、创建类Cat 。实现InitializingBean和DisposableBean 接口
@Component
public class Cat implements InitializingBean,DisposableBean {
public Cat(){
System.out.println("cat constructor...");
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("cat...destroy...");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("cat...afterPropertiesSet...");
}
}
3、测试
@Test
public void test01(){
//1、创建ioc容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("容器创建完成...");
//关闭容器
applicationContext.close();
}
图片.png
网友评论