首先找到 bean的生命周期的起点。就是从BeanDefinitionMap中创建bean实例的开始的地方。
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplicationContext构造函数的refresh()方法。
-----》finishBeanFactoryInitialization(beanFactory); //实例化所有非懒加载的单例。
-----》》beanFactory.preInstantiateSingletons();
※※※※※※※※※※※※※※※※※※※※※※※※※※※
从DefaultListableBeanFactory的beanDefinitionNames(链表:记录的是所有deandefinitionmap中的beanName)获取所有的beanName并且遍历。
判断是单例且是懒加载后 执行getBean()方法。
-----》》》getBean(beanName)-----》》》》doGetBean() 。。。。。。doCreateBean()
一个普通的bean的实例化(单例,没有循环依赖)。
1、Object sharedInstance = getSingleton(beanName)
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//从一级缓存的读取,显然没有
Object singletonObject = this.singletonObjects.get(beanName);
//isSingletonCurrentlyInCreation(beanName)返回false,因为不在创建中。
//如果在创建中,会放入一个set中。这个只有在循环依赖时用到
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
//所以只会返回null;
return singletonObject;
}
2、满足是单例
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
执行getSingleton方法。参数是beanName和一个lambda表达式。这里的getSingleton和上文的不是同一个函数
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized (this.singletonObjects) {
//从一级缓存获取(单例池),返回null
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
........
//放入一个set表示当前的beanName的bean正在实例化,循环依赖时用到,
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
//执行labmda表达式中的createBean方法。
singletonObject = singletonFactory.getObject();
newSingleton = true;
}
.......
if (newSingleton) {
//放入单例池
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}
我们找到这个方法 doCreateBean方法:
if (instanceWrapper == null) {
//这里的instanceWrapper是bean原型的包装。
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
//这里的bean是实例化好的bean原型,其中没有注入过属性和经过AOP
final Object bean = instanceWrapper.getWrappedInstance();
.......
//这里的earlySingletonExposure 是true
//满足单例 && 默认允许循环注入 && 且在创建过程中。上文加入了Set。
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
追踪addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
这里的addSingletonFactory 向三级缓存中放入了一个beanName和一个lambda表达式
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
//向三级缓存中放入了一个beanName和一个lambda表达式
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
之后完成属性注入 和initializeBean()方法。https://blog.csdn.net/wuyuwei/article/details/88252969 (aware 前置后置处理器??)
1. 对实现Aware接口对bean的设置。
2. 依次回调bean后置处理器的postProcessBeforeInitialization方法。
3. 执行实现了InitializingBean或者用户自定义的init方法方法。
4. 依次回调bean后置处理器的postProcessAfterInitialization方法。
最后还会执行一次getSingleton()方法,从三级缓存中取出对象返回。