Bean的生命周期指的是Bean的创建-->初始化-->销毁的一系列过程。
注:对于单实例bean,可以使用;但是对于多实例bean,容器只负责初始化,但不会管理bean,容器关闭时不会调用销毁方法。
public class Dog {
public Dog(){
System.out.println("Dog construct。。。");
}
public void init(){
System.out.println("Dog init。。。");
}
public void destroy(){
System.out.println("Dog destroy。。。");
}
}
测试单实例:
@Configuration
public class MainConfig7 {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Dog dog(){
return new Dog();
}
}
@Test
public void test01() {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MainConfig7.class);
System.out.println("容器加载完成。。。");
//关闭容器
app.close();
}
测试结果:
测试多实例:
@Configuration
public class MainConfig7 {
@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public Dog dog(){
return new Dog();
}
}
@Test
public void test01() {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MainConfig7.class);
System.out.println("容器加载完成。。。");
//多实例只有在使用的时候才会去创建对象
app.getBean("dog");
//关闭容器
app.close();
}
测试结果:
public class Cat implements InitializingBean,DisposableBean {
public Cat(){
System.out.println("Cat construct 。。。");
}
public void afterPropertiesSet() {
System.out.println("Cat afterPropertiesSet 。。。");
}
public void destroy() {
System.out.println("Cat destroy 。。。");
}
}
@Configuration
public class MainConfig7 {
/*@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public Dog dog(){
return new Dog();
}*/
@Bean
public Cat cat(){
return new Cat();
}
}
@Test
public void test01() {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MainConfig7.class);
System.out.println("容器加载完成。。。");
//多实例只有在使用的时候才会去创建对象
// app.getBean("dog");
//关闭容器
app.close();
}
测试结果:
(1)@PostConstruct:构造方法结束,bean创建完成之后,进行初始化
(2)@PreDestroy:在销毁之前调用
public class Fish {
public Fish(){
System.out.println("Fish construct 。。。");
}
@PostConstruct
public void init() {
System.out.println("Fish init 。。。");
}
@PreDestroy
public void destroy() {
System.out.println("Fish destroy 。。。");
}
}
@Configuration
public class MainConfig7 {
/*@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public Dog dog(){
return new Dog();
}*/
/*@Bean
public Cat cat(){
return new Cat();
}*/
@Bean
public Fish fish(){
return new Fish();
}
}
@Test
public void test01() {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MainConfig7.class);
System.out.println("容器加载完成 。。。");
//多实例只有在使用的时候才会去创建对象
// app.getBean("dog");
//关闭容器
app.close();
}
测试结果:
上面主要介绍了初始化和销毁两个方法,spring还提供了一个BeanPostProcessor接口,可以在初始化之前和之后做一些事情,分别实现两个方法:postProcessBeforeInitialization(),bean初始化之前拦截调用;postProcessAfterInitialization(),bean初始化之后调用。
public class MyPostProcessor implements BeanPostProcessor {
/**
* Created by dapeng on 2020/1/1.
* bean初始化之前调用
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization 。。。beanName:"+beanName);
return bean;
}
/**
* Created by dapeng on 2020/1/1.
* bean初始化之后调用
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization 。。。beanName:"+beanName);
return bean;
}
}
@Configuration
public class MainConfig7 {
/*@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public Dog dog(){
return new Dog();
}*/
/*@Bean
public Cat cat(){
return new Cat();
}*/
@Bean
public Fish fish(){
return new Fish();
}
@Bean
public MyPostProcessor myPostProcessor(){
return new MyPostProcessor();
}
}
测试结果: