Bean的执行流程图
Spring中Bean 的完整生命周期,在学习Spring 框架的时候,理解Spring的Bean的管理的非常重要,下图是Spring的Bean构造的全过程:
Bean 的生命周期
-
Spring启动,查找并加载需要被Spring管理的bean,进行Bean的实例化
-
Bean实例化后对将Bean属性注入值
-
如果Bean实现了BeanNameAware接口的话,Spring将调用setBeanName()方法,将Bean的id 传进来
-
如果Bean实现了BeanFactoryAware接口的话,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传进来
-
如果Bean实现了ApplicationContextAware接口的话,Spring将调用Bean的setApplicationContext()方法,将bean所在应用上下文引用传进来。
-
如果Bean实现了BeanPostProcessor接口,Spring就将调用他们的BeanPostProcessor.postProcessBeforeInitialization()方法
-
如果Bean中方法标注注解@PostConstruct,就执行该方法
-
如果Bean 实现了InitializingBean接口,Spring将调用他们的InitializingBean.afterPropertiesSet()方法。同时,如果bean使用init-method声明了初始化方法,该方法也会被调用
-
如果Bean 实现了BeanPostProcessor接口,Spring就将调用他们的BeanPostProcessor.postProcessAfterInitialization()()方法。这时,Bean已经准备创建完成了。他们将一直存在应用上下文中,直到应用上下文被销毁。
-
如果Bean中方法标注注解@PreDestroy,就执行该方法
-
如果bean实现了DisposableBean接口,Spring将调用它的destory()接口方法,同样,如果bean使用了destory-method 声明销毁方法,该方法也会被调用。
代码实例验证Bean 的生命周期
User 类,实现BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean接口
public class User implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String userName;
public User() {
System.out.println("User 被实例化!!!!");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
System.out.println("阿福聊聊编程!!");
this.userName = userName;
}
public void init() {
System.out.println("执行initMethod方法!!!");
}
@PostConstruct
public void postContruct() {
System.out.println("执行@PostConstruct方法");
}
public void destory1() {
System.out.println("执行destroyMethod方法!!!!");
}
@PreDestroy
public void preDestroy() {
System.out.println("执行@PreDestroy 方法!!!!");
}
public void afterPropertiesSet() throws Exception {
System.out.println("执行InitializingBean.afterPropertiesSet()");
}
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy()");
}
public void setBeanName(String name) {
System.out.println("BeanNameAware.setBeanName()" + "--->BeanName:" + name);
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware.setBeanFactory()");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware.setApplicationContext()" );
}
}
定义后置处理器MyBeanPostProcessor,实现postProcessBeforeInitialization,postProcessAfterInitialization的方法
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization()--before->"+"初始化beanName:"+beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessAfterInitialization()--after-->"+"初始化beanName:"+beanName);
return bean;
}
}
定义MyConfigUser,将User ,MyBeanPostProcessor 注册到Spring容器当中,关于Bean注册参考SpringIOC汇总
@ComponentScan(basePackages = "zfcoding.beanpostprocessor")
@Configuration
public class MyConfigUser {
@Bean(initMethod = "init",destroyMethod = "destory1")
public User user(){
return new User();
}
}
单元测试
public class BeanProcessTest {
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(MyConfigUser.class);
User user = applicationContext.getBean(User.class);
applicationContext.close();
}
}
运行结果:
User 被实例化!!!!
BeanNameAware.setBeanName()--->BeanName:user
BeanFactoryAware.setBeanFactory()
ApplicationContextAware.setApplicationContext()
MyBeanPostProcessor.postProcessBeforeInitialization()--before->初始化beanName:user
执行@PostConstruct方法
执行InitializingBean.afterPropertiesSet()
执行initMethod方法!!!
执行@PreDestroy 方法!!!!
DisposableBean.destroy()
执行destroyMethod方法!!!!
文章参考:https://www.cnblogs.com/javazhiyin/p/10905294.html
以上就是Spring Bean 创建的全过程,完事。
我是阿福,公众号「阿福聊编程」作者,对后端技术保持学习爱好者,我会经常更新JAVA技术文章,在进阶的路上,共勉!
欢迎大家关注我的公众号,后台回复666,领取Java全套进阶实战课程。