本文作者:geek,一个聪明好学的同事

1. 简介

开发中我们常用@Commpont,@Service,@Resource等注解或者配置xml去声明一个类,使其成为spring容器中的bean,以下我将用从源码角度看以AnnotationConfigApplicationContext为例看spring如何把带有注解的类生成spring中bean。

2. 示例代码

public class TestContext {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
SingleBean singleBean = context.getBean(SingleBean.class);
System.out.println("<=====>"+singleBean.getTestStr());
}
}
@ComponentScan("com.geek")
public class AppConfig {
}
@Component
public class SingleBean {
private String testStr = "testStr"; public String getTestStr() {
return testStr;
}
}

注意:以上代码仅需要引入spring-context依赖即可。

3. 源码分析

​ 上面的demo在调用AnnotationConfigApplicationContext构造函数的时候,AppConfig类会被注册到AnnotatedBeanDefinitionReader,由这个reader把AppConfig解释为beanDefination,从而被spring获取到要实例化的类信息,以下为bean生产的源码及其注释。(源码基于springFramework 5.1.X)

3.1 创建入口

​ 单例bean的创建的入口为DefaultListableBeanFactory.java#preInstantiateSingletons,下面源码可见创建的bean条件为非抽象,非@LazyInit注解,Scope为singleTon(默认为singleTon)。

@Override
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
logger.trace("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
//所有可能需要去实例化的class(lazy,scope)
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
/**
* 非抽象,非懒初始化,单例bean
*/
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
//非工厂bean实例化
else {
getBean(beanName);
}
}
}
/**
* 实例化完成后触发实现了SmartInitializingSingleton方法的bean
* 的afterSingletonsInstantiated方法
*/
// Trigger post-initialization callback for all applicable beans...
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}

3.2 创建前doGetBean代码逻辑

getBean方法进来后便是直接调用doGetBean,doGetBean执行的源码解释如下:
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
/**
* 获取beanName
* 1,去掉factortoryBean前缀&
* 2,带有别名的bean转换为原来名字
*/
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
/**
* 从DefaultSingletonBeanRegistry的singletonObjects
* (spring内部用来缓存单例bean的currentHashMap)检查是否存在该bean,不存在则创建
* 涉及单例模式下的循环依赖解决
*/
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
/**
*获取给定bean实例的对象,如果是Factory Bean,
* 则可以是bean实例本身或其创建的对象。
*/
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance:
// We're assembly within a circular reference.
/**
* 原型模式下的bean存在循环依赖则会抛异常
*/
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
/**
* 找不到则从父容器中查找
*/
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
/**
* 父容器中也找不到该bean,则需要重新实例化
* 1,获取要实例化bean的beanDefinition,
* 2,检查bean的实例化需要依赖的其他bean
*/
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
/**
* 若给定的依赖 bean 已经注册为依赖给定的bean
*/
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
/**
* 递归调用getBean,优先创建依赖的bean
*/
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// Create bean instance.
if (mbd.isSingleton()) {
/**
* 通过调用DefaultSingletonBeanRegistry的getSingleton,从而调用核心方法createBean创建
*/
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);
} else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
} else {
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, () -> {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
});
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName,
"Scope '" + scopeName + "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex);
}
}
}
catch (BeansException ex) {
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
}
/**
* 检查需要的bean类型是否符合
*/
// Check if required type matches the type of the actual bean instance.
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isTraceEnabled()) {
logger.trace("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}

doGetBean的操作流程如下:

1,执行transformedBeanName方法转换beanName

​ 传递的参数可能是bean的alias或者为FactoryBean,transformedBeanName执行的操作:(1)若传进来的FactoryBean(FactoryBean以&作为前缀标记),去掉&修饰符。(2)经过(1)的处理后,有alias的bean则从aliasMap中获取bean的原始beanName。

2,从容器的缓存中获取bean

​ getSingleton先从spring三级缓存中的第一级singletonObjects(Map结构)中获取,若不存在,则检查该bean是否正在创建isSingletonCurrentlyInCreation(beanName)) ,正在创建的bean会从二级缓存earlySingletonObjects(Map结构)获取。获取到缓存的bean后会调用AbstractBeanFactory#getObjectForBeanInstance转换bean的实例本身返回。因为从缓存中拿到的可能是factoryBean,所以getObjectForBeanInstance需要把是通过从缓存factoryBeanObjectCache获取或通过factory.getObject()获得相应的bean返回。

3,bean实例化前检查

​ (1)先检查是否原型模式下的bean是否存在循环依赖,是则会抛异常。

​ (2)检查父类工厂(parentBeanFactory)是否存在,存在则从parentBeanFactory中递归调用doGetBean。

​ (3)获取改bean的beanDefinition,检查该bean实例化过程中是否涉及依赖了其他的bean,若是则递归调用getBean,优先创建依赖的bean。(涉及单例下的循环以来解决,下篇文章详细介绍)。

​ (4)对创建bean代码加synchronized和执行beforeSingletonCreation(beanName)前置处理。

3.3 创建前createBean逻辑

​ 经过前面的doGetBean的一轮检查与准备后,便在AbstractAutowireCapableBeanFactory#createBean中开始bean的创建。

/**
* Central method of this class: creates a bean instance,
* populates the bean instance, applies post-processors, etc.
* 解析指定 BeanDefinition 的 class
* 处理 override 属性
* 实例化的前置处理
* 创建 bean
* @see #doCreateBean
*/
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException { if (logger.isTraceEnabled()) {
logger.trace("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd; // Make sure bean class is actually resolved at this point, and
// clone the bean definition in case of a dynamically resolved Class
// which cannot be stored in the shared merged bean definition.
/**
* 解释bean的class,看beanDefinition是否有class,否则load class
*/
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
/**
* 对bean不存在lookup-method 和 replace-method
* 标记其方法的overloaded为false
*/
// Prepare method overrides.
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
} try {
/**
* 调用实现实现BeanPostProcessor的bean后置处理生成代理对象,
* 有代理对象则直接返回代理对象
*/
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
} try {
/**
* 无需代理的bean实例化
*/
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
// A previously detected exception with proper bean creation context already,
// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
}
}

createBean的操作流程如下:

1,resolveBeanClass

​ 解释beanDefinition的class,并且保存在beanDefinition中。

2,prepareMethodOverrides

​ 处理bean中的lookup-method (在单例bean用 @Lookup注解标记的方法,注解的方法返回的对象是原型)和 replace-method( 标记的方法,标记bean中A的方法被实现被另外一个实现MethodReplacer接口的B方法替代)。

3,resolveBeforeInstantiation

​ 调用实现实现BeanPostProcessor的bean后置处理生成代理对象,有代理对象则直接返回代理对象。(spring AOP则是基于此处实现)

3.4 bean的真正实例化createBeanInstance

​ 在AbstractAutowireCapableBeanFactory#createBeanInstance中,真正创建bean,源码及注释如下:

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// Make sure bean class is actually resolved at this point.
Class<?> beanClass = resolveBeanClass(mbd, beanName);
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
/**
* 通过提供supplier回调方法创建
*/
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
/**
* 通过工厂方法创建 bean 实例,可以是静态工厂方法或者实例工厂
*/
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// Shortcut when re-creating the same bean...
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
/**
* 查找已经bean已经缓存解析的构造函数或者工厂方法
*/
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
/**
* 已经有缓存的构造函数或者工厂方法,直接实例化
*/
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {
return instantiateBean(beanName, mbd);
}
}
/**
* 通过实现BeanPostProcessor的代理类autowired的构造函数实例化
*/
// Candidate constructors for autowiring?
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
/**
* 通过本身带有autowired的构造函数实例化,通过调用反射newInstance实现
*/
// Preferred constructors for default construction?
ctors = mbd.getPreferredConstructors();
if (ctors != null) {
return autowireConstructor(beanName, mbd, ctors, null);
}
/**
* 无参构造函数实例化,通过调用反射newInstance实现
*/
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
}

由上述源码可以看出,实例化bean操作流程如下:

1,如果存在 Supplier 回调,则通过提供supplier回调方法创建,如以下方式定义的bean:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class BeanRegistrationTest {
@Autowired
private GenericWebApplicationContext context; context.registerBean(A.class, () -> new A());
}

2,如果存在工厂方法,则通过工厂方法创建 bean 实例,可以是静态工厂方法或者实例工厂,如以下方式定义的bean:

public class AFactory implements FactoryBean<A> {
@Override
public A getObject() throws Exception {
return new A();
}
@Override
public Class<?> getObjectType() {
return A.class;
}
}
//或:
@Configuration
public class BeanConfigration {
@Bean
public A a(){
return new A();
} }

3,已经有缓存的构造函数或者工厂方法,直接实例化。

4,以上三点都不存在,则使用带参构造函数与无参构造函数实例化。如以下方式定义的bean:

@Commponet
public class A{}

4.总结

​ spring单例bean的实例化流程大概就是这样,很多细节地方,包括循环依赖处理,bean属性填充等细节点下一章介绍。

参考

查看更多文章关注公众号:好奇心森林

Spring 源码学习 - 单例bean的实例化过程的更多相关文章

  1. Spring 源码学习——加载 Bean

    继上次注册 bean 之后好久没更新,这两天有空查了查资料也自己看了看 spring BeanFactory 的 getBean(beanName); 这个方法.因时间有限不能像之前那样复制代码并一行 ...

  2. Spring源码学习笔记之bean标签属性介绍及作用

    传统的Spring项目, xml 配置bean在代码中是经常遇到, 那么在配置bean的时候,这些属性的作用是什么呢? 虽然说现在boot项目兴起,基于xml配置的少了很多, 但是如果能够了解这些标签 ...

  3. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  4. spring源码学习之路---深入AOP(终)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...

  5. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

  6. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  7. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  8. spring源码学习之路---IOC初探(二)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...

  9. Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory

    前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...

随机推荐

  1. css3及css技巧

    左右对齐:  

  2. Django之AJAX简单使用

    AJAX简介: AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步的Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传输 ...

  3. [翻译]用于.NET Core的Windows窗体设计器发布

    本文由微信公众号<开发者精选资讯>翻译首发,转载请注明来源 今天我们很高兴地宣布,.NET Core项目的Windows窗体设计器现在可以在 Visual Studio 2019 16.6 ...

  4. Java的集合(一)

    转载:https://blog.csdn.net/hacker_zhidian/article/details/80590428 Java集合概况就三个:List.set和map list(Array ...

  5. AVL树的创建--C语言实现

    AVL树是一种自平衡(Self-balancing)二叉查找树(Binary Search Tree),要求任何一个节点的左子树和右子树的高度之差不能超过1. AVL树的插入操作首先会按照普通二叉查找 ...

  6. F. Machine Learning 带修端点莫队

    F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  7. linux查看磁盘使用情况命令

    第一:统一磁盘整体情况,包括磁盘大小,已使用,可用1.查看当前目录命令 df -h1结果: 统一每个目录下磁盘的整体情况 2.查看指定目录在命令后直接放目录名,比如查看“usr”目录使用情况: df ...

  8. Linux的运行等级与目标

    在老的 Linux 发行版本中,系统运行分成不同的运行级别(run level),不同的级别所启动的服务搭配有所不同.较新的 Linux 发行版本,比如 CentOS 7+,已经将运行级别替换成另一个 ...

  9. 谈谈MySQL 索引

    1.索引是什么 索引(Index)是帮助MySQL高效获取数据的数据结构.我们可以简单理解为:索引的目的在于提高查询效率. 2.原理 索引的数据结构是B+树,原理图如下 关于B+树的详细介绍,可以参见 ...

  10. 获取MP4媒体文件时长

    由于之前上传MP4文件没有保存视频时长,现在有需要,所以只好写代码读取时长.找了几个发现是 c/c++ 实现,或者是借助 FFmpeg 实现. 一个偶然在 GitHub 上面发现一个 c 文件,由于获 ...