一、说明
- 1.bean的生命周期指的是bean从创建到销毁的整个过程。
- 2.spring对bean进行实例化。
- 3. spring将值和bean的引用注入到bean对应的属性中。
- 4.如果bean实现了BeanNameAware接口,spring将bean的ID传递给setBean-Name()方法。
- 5.如果bean实现了BeanFactoryAware接口,spirng将调用setBeanFactory()方法,将BeanFactory容器实例传入。
- 6.如果bean实现了ApplicationContextAware接口,spring将调用setApplicationContext()方法,将bean所在的应用上下文的引用传入。
- 7.如果bean实现了BeanPostProcessor接口,spring将调用其postProcessBeforeInitialization()方法。
- 8.如果bean实现了InitializingBean接口,spring将调用其afterPropertiesSet()方法。如果bean使用init-method声明了初始化方法,则调用初始化方法。
- 7.如果bean实现了BeanPostProcessor接口,spring将调用其postProcessAfterInitialization()方法。
- 8.bean已经准备就绪,可以被程序使用,将一直在应用上下文中,直到应用上下文被销毁。
- 9.如果bean实现了DisposableBean接口,spring将调用其destroy()接口方法。如果bean使用destroy-method声明了销毁方法,则调用销毁方法。
二、四步
- 1.实例化。
- 2. 属性赋值。
- 3.初始化。
- 4.销毁。
2.1 实例化
- 1. 通过反射去推断构造函数进行实例化。
- 2. 通过实例工厂、静态工厂实例化。
2.2 属性赋值
- 1. 解析自动装配(byname、bytype、constructor、none、@autowired)。
- 2. 循环依赖。
2.3 初始化
- 1. 调用xxxAware回调方法(BeanNameAware、BeanClassLoaderAware、BeanFactoryAware)。
- 2. 调用初始化生命周期回调(三种:@PostConstruct、实现InitiallizingBean接口、@Bean(initMethod = “initMethod”))。
- 3. 如果bean实现aop,创建动态代理。
2.4 销毁
- 1. 在spring容器关闭的时候进行调用。
- 2. 调用销毁生命周期回调(三种:@PreDestroy、实现DisposableBean接口、@Bean(initMethod = “destroyMethod”))。
三、最全生命周期
3.1 代码示例
package com.learning.bean_life_cycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
/**
* 实例化执行之前,这里返回的对象会替换掉原本的Bean
* @param beanClass the class of the bean to be instantiated
* @param beanName the name of the bean
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if(beanName.equals("lifeCycleBean")){
System.out.println("spring接口->org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation: 实例化执行之前,这里返回的对象会替换掉原本的Bean");
}
// 返回null的话保持原有bean不变
return null;
}
/**
* 实例化之后执行,如果返回false会跳过依赖注入阶段
* @param bean the bean instance created, with properties not having been set yet
* @param beanName the name of the bean
* @return
* @throws BeansException
*/
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if(beanName.equals("lifeCycleBean")){
System.out.println("spring接口->org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation: 实例化之后执行,如果返回false会跳过依赖注入阶段");
}
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
if(beanName.equals("lifeCycleBean")){
System.out.println("spring接口->org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties: 依赖注入阶段执行,如@Autowired、@Value、@Resource");
}
return pvs;
}
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if(beanName.equals("lifeCycleBean")){
System.out.println("spring接口->org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction: 销毁之前执行,如@PreDestroy");
}
}
@Override
public boolean requiresDestruction(Object bean) {
return DestructionAwareBeanPostProcessor.super.requiresDestruction(bean);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("spring接口->org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization: 初始化之前执行,这里返回的对象会替换掉原本的 bean,如 @PostConstruct、@ConfigurationProperties");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("spring接口->org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization: 初始化之后执行,这里返回的对象会替换掉原本的 bean,如代理增强");
}
return bean;
}
}
package com.learning.bean_life_cycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//import org.springframework.stereotype.Component;
//@Component
public class LifeCycleBean implements InitializingBean, DisposableBean {
public LifeCycleBean(){
System.out.println("construction method: LifeCycleBean的构造方法进行实例化");
}
@Autowired
public void autowire(@Value("${JAVA_HOME}") String home){
System.out.println("spring注解->@org.springframework.beans.factory.annotation.Autowired: 依赖注入->" + home);
}
@PostConstruct
public void init(){
System.out.println("jdk注解->@javax.annotation.PostConstruct: 初始化方法调用");
}
// 单例bean调用销毁方法
@PreDestroy
public void destory(){
System.out.println("jdk注解->@javax.annotation.PreDestroy: 销毁之前调用");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("spring接口->org.springframework.beans.factory.InitializingBean: 初始化之后调用");
}
@Override
public void destroy() throws Exception {
System.out.println("spring接口->org.springframework.beans.factory.DisposableBean: 销毁之前调用");
}
private void initMehtod() {
System.out.println("spring注解->org.springframework.context.annotation.Bean.initMethod: 初始化方法");
}
private void destroyMethod() {
System.out.println("spring注解->org.springframework.context.annotation.Bean.destroyMethod: 销毁方法");
}
}
package com.learning.bean_life_cycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//作为配置类,代替xml文件
@ComponentScan(basePackages = {"com.learning.bean_life_cycle"})
public class SpringConfig {
@Bean(initMethod = "initMehtod", destroyMethod = "destroyMethod")
public LifeCycleBean lifeCycleBean(){
return new LifeCycleBean();
}
}
package com.learning.bean_life_cycle;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
LifeCycleBean lifeCycleBean = annotationConfigApplicationContext.getBean(LifeCycleBean.class);
// 关闭容器
System.out.println("bean可以被使用"+lifeCycleBean);
annotationConfigApplicationContext.close();
}
}
3.2 执行结果
spring接口->org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation: 实例化执行之前,这里返回的对象会替换掉原本的Bean
construction method: LifeCycleBean的构造方法进行实例化
spring接口->org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation: 实例化之后执行,如果返回false会跳过依赖注入阶段
spring接口->org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties: 依赖注入阶段执行,如@Autowired、@Value、@Resource
spring注解->@org.springframework.beans.factory.annotation.Autowired: 依赖注入->C:\Program Files\Java\jdk1.8.0_251
spring接口->org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization: 初始化之前执行,这里返回的对象会替换掉原本的 bean,如 @PostConstruct、@ConfigurationProperties
jdk注解->@javax.annotation.PostConstruct: 初始化方法调用
spring接口->org.springframework.beans.factory.InitializingBean: 初始化之后调用
spring注解->org.springframework.context.annotation.Bean.initMethod: 初始化方法
spring接口->org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization: 初始化之后执行,这里返回的对象会替换掉原本的 bean,如代理增强
bean可以被使用com.learning.bean_life_cycle.LifeCycleBean@26e356f0
spring接口->org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction: 销毁之前执行,如@PreDestroy
jdk注解->@javax.annotation.PreDestroy: 销毁之前调用
spring接口->org.springframework.beans.factory.DisposableBean: 销毁之前调用
spring注解->org.springframework.context.annotation.Bean.destroyMethod: 销毁方法