没有合适的资源?快使用搜索试试~ 我知道了~
所有Bean创建的真正逻辑都在doCreateBean函数中,本篇把重点放在第一点实例化bean上,先省略其他无关代码。 bean的实例化实质就是调用构造函数的过程。构造函数分为有参数的构造函数和无参数的构造函数,对比起无参数构造函数,有参数构造函数将大量的工作花在了挑选最合适的构造函数的过程中,并非是核心所在,因此本篇只讲无参数的构造函数 // AbstractAutowireCapableBeanFactory.java protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, fi
资源详情
资源评论
资源推荐
spring源码源码14: 实例化实例化 无参构造器无参构造器
所有Bean创建的真正逻辑都在doCreateBean函数中,本篇把重点放在第一点实例化bean上,先省略其他无关代码。
bean的实例化实质就是调用构造函数的过程。构造函数分为有参数的构造函数和无参数的构造函数,对比起无参数构造函数,有参数
构造函数将大量的工作花在了挑选最合适的构造函数的过程中,并非是核心所在,因此本篇只讲无参数的构造函数
// AbstractAutowireCapableBeanFactory.java
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
// 是单例的情况下清空缓存
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
/**
* 1. 实例化bean,这一阶段是调用构造器生产实例的阶段
*
* 如果有使用@Autowired构造器注入,则在该阶段完成属性的注入。
* (注意这里是Autowired构造器,而不是我们正常使用的注解在属性上)
* @Autowired
* public Outer(Inner inner) {
* this.inner = inner;
* }
*/
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
...
// 填充、初始化
}
创建实例创建实例
// AbstractAutowireCapableBeanFactory.java
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// Make sure bean class is actually resolved at this point.
// 确保Class已经被解析,没Class还实例化给龟龟
Class beanClass = resolveBeanClass(mbd, beanName);
// beanClass不为空,且beanClass的修饰符不是public,且非公共构造函数和方法不允许访问
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());
}
// spring5的新特性
Supplier instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
// 有工厂方法,直接使用工厂方法
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// Shortcut when re-creating the same bean...
// 是否已经解析过的标志位
boolean resolved = false;
boolean autowireNecessary = false;
/**
* 1. 只有显式参数为空的情况下才尝试使用解析过的缓存,否则一律重新匹配寻找合适的构造方法
* 这里指的不是xml或者注解注入所配置的参数,这里指用户指定显式调用xmlBeanFactory.getBean("dog", "wang");
* 主要是因为xml配置文件和注解spring一旦启动解析完不会变了,而用户显式指定的参数spring根本琢磨不透用户待会想怎么
调用
weixin_38693311
- 粉丝: 4
- 资源: 923
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0