// 忽略了无关代码
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (instanceWrapper == null) {
// 实例化阶段!
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
// 属性赋值阶段!
populateBean(beanName, mbd, instanceWrapper);
// 初始化阶段!
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
Aware 分为 Group 1 和 Group 2:
Aware Group 1 包含 BeanNameAware, BeanClassLoaderAware, BeanFactoryAware
Aware Group 2 包含 EnvironmentAware, EmbeddedValueResolverAware 和
ApplicationContextAware(ResourceLoaderAware\ApplicationEventPublisherAware\MessageSourceAware)

Bean Definition 是 Spring Framework 中定义的Bean的配置元信息接口, 包含4个部分:
1. Bean 的 Class Name (类名)
2. Bean 的 行为配置元素, 包括: 作用域, 自动绑定的模式, 生命周期回调等
3. Bean 的 Collaborators 或者 Dependencies (Bean的引用)
4. Bean 的 Properties (配置设置)
初始化实现: 1. 通过 Java 的 @PostConstruct
2. 通过 Spring 的 InitializingBean 接口, 然后 Override afterPropertiesSet方法
3. 通过 Spring 的 XML Bean 配置 或 Bean的注解 指定初始化方法
销毁前后实现:
1. 通过 Java 的 @PreDestroy
2. 通过 Spring 的 DisposableBean 接口, 然后 Override destroy 方法
3. 通过 Spring 的 XML Bean 配置 或 Bean的注解 指定销毁方法
完整的 初始化 和 销毁 顺序 是:
初始化: Class Constructor ------ @PostConstruct ------ InitializingBean ------ init-method
销毁 : @PreDestroy ------ DisposableBean ------ destroyMethod
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。