简单整理下 spring bean 的加载

createBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// AbstractAutowireCapableBeanFactory.java#createBean(xxx)
// ...
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
// 用 BeanPostProcessors 返回代理来替代真正的实例(如果 Bean 配置了 PostProcessor,那么这里返回的是一个代理)
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
// ...
try {
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isDebugEnabled()) {
logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
// ...

doCreateBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// AbstractAutowireCapableBeanFactory.java#doCreateBean(xxx)

// ...
// 创建Bean实例
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
//...

// Allow post-processors to modify the merged bean definition.
// 初始化前调用 post-processors,可以让我们在 bean 实例化之前做一些定制操作
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
// 使用 MergedBeanDefinitionPostProcessor,Autowired 注解就是通过此方法实现类型的预解析
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}

// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
// 解决循环依赖问题(只解决单例Bean)
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isTraceEnabled()) {
logger.trace("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}

Object exposedObject = bean;
try {
// 填充属性
populateBean(beanName, mbd, instanceWrapper);
// 初始化 bean 对象
exposedObject = initializeBean(beanName, exposedObject, mbd);
}

// ...

initializeBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// AbstractAutowireCapableBeanFactory.java#initializeBean(xxx)

//...

// 注入 aware,比如 BeanNameAware,BeanClassLoaderAware,BeanFactoryAware
invokeAwareMethods(beanName, bean);

// 调用 BeanPostProcessor 的 postProcessBeforeInitialization 方法
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}

//...

// 调用 bean 的 初始化方法
// 如果是 InitializingBean,会先调用 afterPropertiesSet() 方法
// 如果有 自定义的 init 方法,会再次调用自定义的 init 方法
invokeInitMethods(beanName, wrappedBean, mbd);

// 调用 BeanPostProcessor 的 postProcessAfterInitialization 方法
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}

return wrappedBean;

bean 大致的加载流程就是这样,上面的一些方法比如 createBeanInstance(), populateBean() 等代码都没有贴出来,可以去阅读源码查看。


附一张 bean 的生命周期图