bean的生命周期
- bean的生命周期:
bean的创建—初始化—销毁的过程
容器管理bean的生命周期:
我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
指定初始化和销毁方法:指定init-method和destory-method
指定初始化和销毁的方法:
通过@Bean指定init-method和destory-method;
bean:
public class Car {
public void init() {
System.out.println("Car...init...");
}
public void destory() {
System.out.println("Car...destory...");
}
}
配置类:
@Configuration
public class MyConfig02 {
@Bean(initMethod = "init",destroyMethod = "destory")
public Car car() {
return new Car();
}
}
测试:
public class MyTest01 {
@Test
public void test01() {
// 初始化容器
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MyConfig02.class);
System.out.println("容器启动完成...");
annotationConfigApplicationContext.close();
}
}
结果:容器启动的时候,执行了car的init方法,容器关闭的时候执行destory方法
Car...init...
容器启动完成...
Car...destory...
通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义销毁逻辑)
public class Cat implements InitializingBean, DisposableBean {
public void afterPropertiesSet() throws Exception {
System.out.println("Cat...afterPropertiesSet...");
}
public void destroy() throws Exception {
System.out.println("Cat...destroy...");
}
}
可以使用JSR250(@PostConstruct,@PreDestory)
- @PostConstruct:在bean创建完成并且属性赋值完成,来执行初始化
- @PreDestory:在容器销毁bean之前通知我们进行清理工作
@Component
public class Dog {
public Dog() {
System.out.println("dog...constructor");
}
@PostConstruct
public void init() {
System.out.println("dot...inti...");
}
@PreDestroy
public void destory() {
System.out.println("Dog...destory...");
}
}
BeanPostProcessor
-
只要实现了BeanPostProcessor,并且实现了它的2个方法,所有的bean在初始化前后都会执行执行该2个方法
-
BeanPostProcessor是一个接口,bean的后置处理器:在bean初始化前后进行一些处理工作,它定义了2个方法:
postProcessBeforeInitialization
:在初始化之前工作
postProcessAfterInitialization
:在初始化之后工作这2个方法的返回值是的bean实例。
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("MyBeanPostProcessor...postProcessBeforeInitialization..." + "----->"+ o.getClass()+"---"+s);
return o;
}
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("MyBeanPostProcessor...postProcessAfterInitialization..."+ "---->" + o.getClass()+"----"+s);
return o;
}
}
测试结果:
Dog...constructor
MyBeanPostProcessor...postProcessBeforeInitialization...----->class com.yajun.spring02.bean.Dog---dog
Dog...inti...
MyBeanPostProcessor...postProcessAfterInitialization...---->class com.yajun.spring02.bean.Dog----dog
MyBeanPostProcessor...postProcessBeforeInitialization...----->class com.yajun.spring02.bean.Car---car
Car...init...
MyBeanPostProcessor...postProcessAfterInitialization...---->class com.yajun.spring02.bean.Car----car
MyBeanPostProcessor...postProcessBeforeInitialization...----->class com.yajun.spring02.bean.Cat---cat
Cat...afterPropertiesSet...
MyBeanPostProcessor...postProcessAfterInitialization...---->class com.yajun.spring02.bean.Cat----cat
六月 05, 2019 3:22:41 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@afffde: startup date [Wed Jun 05 15:22:40 CST 2019]; root of context hierarchy
容器启动完成...
Cat...destroy...
Car...destory...
Dog...destory...
spring底层对BeanPostProcessor的使用:
bean的赋值,注入其他组件,@Autowired,生命周期注解功能,