-
Bean 容器找到配置文件中 Spring Bean 的定义。
-
Bean 容器利用 Java Reflection API 创建一个Bean的实例。
-
如果涉及到一些属性值 利用
set()方法设置一些属性值。 -
如果 Bean 实现了
BeanNameAware接口,调用setBeanName()方法,传入Bean的名字。 -
如果 Bean 实现了
BeanClassLoaderAware接口,调用setBeanClassLoader()方法,传入ClassLoader对象的实例。 -
如果Bean实现了
BeanFactoryAware接口,调用setBeanClassLoader()方法,传入ClassLoader对象的实例。 -
与上面的类似,如果实现了其他
*.Aware接口,就调用相应的方法。 -
如果有和加载这个 Bean 的 Spring 容器相关的
BeanPostProcessor对象,执行postProcessBeforeInitialization()方法 -
如果Bean实现了
InitializingBean接口,执行afterPropertiesSet()方法。 -
如果 Bean 在配置文件中的定义包含 init-method 属性,执行指定的方法。
-
如果有和加载这个 Bean的 Spring 容器相关的
BeanPostProcessor对象,执行postProcessAfterInitialization()方法 -
当要销毁 Bean 的时候,如果 Bean 实现了
DisposableBean接口,执行destroy()方法。 -
当要销毁 Bean 的时候,如果 Bean 在配置文件中的定义包含 destroy-method 属性,执行指定的方法。
Spring 中的 bean 生命周期
最新推荐文章于 2021-02-23 17:15:23 发布
本文深入解析Spring框架中Bean的创建过程及生命周期管理。从读取配置文件定义开始,到实例化、属性注入、初始化、销毁等阶段,详细介绍各阶段涉及的接口与方法,帮助读者全面理解Spring Bean的工作机制。
1582

被折叠的 条评论
为什么被折叠?



