调用方
/org/springframework/beans/factory/support/AbstractBeanFactory.java:333
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;
}
});
函数式接口
@FunctionalInterface
public interface ObjectFactory<T> {
/**
* Return an instance (possibly shared or independent)
* of the object managed by this factory.
* @return the resulting instance
* @throws BeansException in case of creation errors
*/
T getObject() throws BeansException;
}
查看代码时的一点注意事项
通过ObjectFactory#getObject方法的实现是找不到具体的执行代码的,
只能通过方法DefaultSingletonBeanRegistry#getSingleton的调用来源去找具体的执行代码
个人的一点理解:
通过函数式编程,我们可以在不传递参数的前提下完成方法的回调
使得代码更加整洁
这里有函数式接口的一些介绍
Java特性函数式接口注解@FunctionalInterface浅析-CSDN博客