Spring中Bean 的完整生命周期,在学习Spring 框架的时候,理解Spring的Bean的管理的非常重要,下图是Spring的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/javaz...
以上就是Spring Bean 创建的全过程,完事。
我是阿福,公众号「阿福聊编程」作者, 对后端技术保持学习爱好者,我会经常更新JAVA技术文章 ,在进阶的路上,共勉!
欢迎大家关注我的公众号,后台回复 666 ,领取Java全套进阶实战课程。