spring bean生命周期指的是:bean的实例化、初始化、使用、销毁
通常来说很多时候我们都需要在bean的初始化以及销毁的时候执行一些方法,比如数据连接需要在bean初始化的时候添加属性,在销毁的时候关闭连接等
spring提供了以下几种方式:
- xml配置方式
- @Bean注解指定
- 实现InitializingBean和DisposableBean
- 使用注解@PostConstruct和@PreDestroy
- BeanPostProcessor
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="color" class="com.lhf.lifecycle.beans.Color" init-method="init" destroy-method="destroy"/>
</beans>
通过init-method属性配置配置bean初始化方法,destroy-method属性配置bean销毁的方法,注意配置bean初始化方法和销毁方法不能有参数
@Bean
通过@Bean注解注入的实例和通过xml配置效果等同,所以配置方式也是类似的:
@Bean(initMethod = "init", destroyMethod = "destroy")
public Black black() {
return new Black();
}
InitializingBean 和 DisposableBean
InitializingBean源码如下:
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
DisposableBean源码如下:
public interface DisposableBean {
void destroy() throws Exception;
}
这两个接口很简单,InitializingBean只定义了afterPropertiesSet方法,他定义为在配置加载完成后执行,DisposableBean只定义了destroy,也就是bean销毁时执行
应用如下:
@Component
public class Green implements InitializingBean, DisposableBean {
public Green() {
System.out.println("constructor....");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("init....");
}
@Override
public void destroy() {
System.out.println("destroy....");
}
}
@PostConstruct和PreDestroy
public class Red {
@PostConstruct
public void init() {
System.out.println("init ....");
}
@PreDestroy
public void destroy() {
System.out.println("destroy ....");
}
}
@PostConstruct和@PreDestroy是javaee api提供 前者代表初始化执行,后者表示销毁时执行
BeanPostProcessor
BeanPostProcessor为bean后置处理器,他只在bean初始化前后执行,源码如下:
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;
}
}
具体使用:
@Component
public class ColorBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Color) {
System.out.println("初始化bean之前:" + beanName);
return bean;
}
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Color) {
System.out.println("初始化bean之后:" + beanName);
return bean;
}
return null;
}
}
一个简单的示例,就是验证bean类型是否为Color,是返回,否返回null, 如果为null ,则不会调用后续的 BeanPostProcessors
通过源码也可以看到调用方式:
1.执行BeanPostProcessor的postProcessBeforeInitialization
2.执行初始化方法
3.执行BeanPostProcessor的postProcessAfterInitialization
BeanPostProcessors是一个调用链,可以有多个
在源码上不难发现,它是调用了,getBeanPostProcessors()获取所有的BeanPostProcessors循环执行,如果执行postProcessBeforeInitialization或者postProcessAfterInitialization得到的返回值为null则直接返回,不会继续执行后续的BeanPostProcessor了
写的有缺陷,请大佬指教!!!