springboot bean对象生命周期
程序员文章站
2022-06-06 11:21:07
...
Bean对象生命周期
Bean对象的生命周期主要由3部分组成:bean对象的初始化过程,bean对象在ioc容器中被使用,bean对象的销毁过程,具体可见下图:
初始化bean对象过程
方法 | 描述 |
---|---|
bean对象实例化 | 调用构造函数为对象分配内存空间 |
setBeanName方法调用 | 如果对象实现了BeanNameAware接口,则调用该方法 |
setBeanFactory方法调用 | 如果对象实现了BeanFactoryAware接口,则调用该方法 |
setApplicationContext方法调用 | 如果实现了ApplicationContextAware接口,且spring ioc容器也是ApplicationContext的接口实现类,则调用该方法 |
postProcessBeforeInitialization方法调用 | 如果实现了BeanPostProcessor接口,则调用该方法 |
postConstruct注解方法调用 | 如果bean对象定义了@PostConstruct注解的方法,则调用该方法 |
afterPropertiesSet方法调用 | 如果实现了InitializingBean接口,则调用该方法 |
自定义init-method方法调用 | 如果设置了自定义初始化方法,则调用自定义初始化方法 |
postProcessAfterInitialization方法调用 | 如果实现了BeanPostProcessor接口,则调用该方法 |
销毁bean对象过程
方法 | 描述 |
---|---|
PreDestroy注解方法调用 | 如果bean对象定义了@PreDestroy注解的方法,则调用该方法 |
destory方法调用 | 如果实现了DisposableBean接口,则调用该方法 |
自定义destroyMethod方法调用 | 如果自定义了销毁方法,则调用该方法 |
代码实例
下面的代码定义了一个bean类SpringLifeCycleBean用来对bean对象的生命周期进行测试,将上述不同阶段的方法都封装在该类中。
package com.mary;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Created by gaoling on 2020/10/10.
*/
public class SpringLifeCycleBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor, DisposableBean{
public SpringLifeCycleBean(){
System.out.println("Constructor method invoked");
}
@Override
public void setBeanName(String s) {
System.out.println("BeanNameAware setBeanName method inovked, name: " + s);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware setBeanFactory method inovked, beanFactory: " + beanFactory.getClass().getName());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware setApplicationContext method inovked, applicationContext: " + applicationContext.getClass().getName());
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor postProcessBeforeInitialization method inovked, beanName: " + beanName);
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor postProcessAfterInitialization method inovked, beanName: " + beanName);
return null;
}
@PostConstruct
public void init(){
System.out.println("PostConstruct method invoked");
}
@PreDestroy
public void preDestroy(){
System.out.println("PreDestroy method invoked");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean destroy method invoked");
}
public void customInit(){
System.out.println("customInit method invoked");
}
public void customDestroy(){
System.out.println("customDestroy method invoked");
}
}
package com.mary;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/**
* Created by gaoling on 2020/10/10.
*/
@SpringBootApplication
public class AppStarter {
public static void main(String[] args){
SpringApplication.run(AppStarter.class, args);
}
@Bean(destroyMethod = "customDestroy", initMethod = "customInit")
public SpringLifeCycleBean lifeCycleBean(){
SpringLifeCycleBean lifeCycleBean = new SpringLifeCycleBean();
return lifeCycleBean;
}
}
代码执行结果如下
Constructor method invoked
BeanPostProcessors (for example: not eligible for auto-proxying)
BeanNameAware setBeanName method inovked, name: lifeCycleBean
BeanFactoryAware setBeanFactory method inovked, beanFactory: org.springframework.beans.factory.support.DefaultListableBeanFactory
ApplicationContextAware setApplicationContext method inovked, applicationContext: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
PostConstruct method invoked
InitializingBean afterPropertiesSet method invoked
customInit method invoked
BeanPostProcessor postProcessBeforeInitialization method inovked, beanName: org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
BeanPostProcessor postProcessAfterInitialization method inovked, beanName: org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat