Bean的生命周期:
bean的创建------>初始化------>销毁的过程
由容器来管理bean的生命周期
我们可以自定义bean的初始化和销毁方法,让容器在bean进行到当前生命周期时来调用这些方法。
构造(对象创建)
单实例:在容器启动的时候创建
多实例:在每一次获取的时候创建
初始化:
对象创建完成,并赋值好,调用初始化方法
销毁:
单实例:在容器关闭的时候关闭
多实例:容器不管理这个bean,容器不会调用销毁方法
1.指定初始化和销毁方法
通过@Bean的initMethod和destroyMethod指定初始化方法和销毁方法
@Bean(initMethod = "init",destroyMethod = "destroy")
单实例:
测试类:
package cn.it.bean; /** * @author Admin * @create 2019-04-21-23:18 */ public class Car { public Car() { System.out.println("Car.....创建"); } public void init(){ System.out.println("Car.....初始化方法"); } public void destroy(){ System.out.println("Car.....销毁方法"); } }
@Test void BeanTest() { AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(MainConfigOfLife.class); System.out.println("容器创建完成"); ioc.close(); System.out.println("容器关闭"); }
打印结果:
通过多实例:
2.通过让Bean实现InitializingBean,DisposableBean接口
Cat类实现了 InitializingBean,DisposableBean接口
public class Cat implements InitializingBean,DisposableBean { public Cat() { System.out.println("Cat.....constructor..."); } public void destroy() throws Exception { System.out.println("Cat.....destroy......"); } public void afterPropertiesSet() throws Exception { System.out.println("Cat....afterPropertiesSet......."); } }
测试:
3. 可以使用JSR250:@PostConstruct和@PreDestroy注解
4.使用Spring的后置处理器BeanPostProcessor
public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Nullable default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }
postProcessBeforeInitialization:在bean初始化之前工作
postProcessAfterInitialization:在bean初始化之后工作
写好实现了BeanPostProcessor 的类
/** * 后置处理起,初始化前后进行处理 * @author Admin * @create 2019-04-26-16:29 */ @Component public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization..."+beanName+":::"+bean); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization..."+beanName+"---"+bean); return bean; } }
将后置处理器加入到容器中
测试:
BeanPostProcessor的原理
源码:
在初始化之前调用:applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName)
@Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
遍历得到容器中所有的 BeanPostProcessor,挨个执行postProcessBeforeInitialization方法,一但方法返回null,跳出for循环,不会执行后面的beanProcessor.postProcessBeforeInitialization(result, beanName)。
执行bean的初始化方法
invokeInitMethods(beanName, wrappedBean, mbd)
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName)
@Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { Object current = beanProcessor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }