Spring源码分析-bean的初始化过程
2021-05-02 01:29
阅读:568
标签:post with name rsa ring 注解 on() sso ESS
初始化过程
- 构造 bean
- 依赖注入(接下来才是初始化过程)
- 获取容器中所有的 BeanPostProcessor,执行其 postProcessBeforeInitialization 方法(@PostConstruct注解指定的初始化方法,就是在这一步由 CommonAnnotationBeanPostProcessor 实现)
- 如果 bean 实现了 InitializingBean 接口,执行 afterPropertiesSet 方法
- 如果 @Bean 注解中指定了 initMethod 属性,执行指定的方法
- 获取容器中所有的 BeanPostProcessor,执行其 postProcessAfterInitialization 方法
源码分析
- BeanFactory 的 initializeBean 方法中
/**
* 获取 BeanPostProcessor,执行 postProcessBeforeInitialization 方法
*/
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
/**
* 执行 bean 指定的初始化方法:InitializingBean 接口的 afterPropertiesSet 方法,@Bean 注解指定的初始化方法
*/
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
/**
* 获取 BeanPostProcessor,执行 postProcessAfterInitialization 方法
*/
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
- invokeInitMethods 方法
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking afterPropertiesSet() on bean with name ‘" + beanName + "‘");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged((PrivilegedExceptionAction
Spring源码分析-bean的初始化过程
标签:post with name rsa ring 注解 on() sso ESS
原文地址:https://www.cnblogs.com/lighter-blog/p/13205669.html
评论
亲,登录后才可以留言!