Spring源码(三) Spring IoC之BeanFactory创建bean和获取bean

ApplicationContext 和 BeanFactory

可以先简单了解下 ApplicationContext 和 BeanFactory。

详情见: https://blog.csdn.net/sinat_32502451/article/details/140247662

getBean() 代码示例

首先看代码示例,调试一下。

代码详情见: https://blog.csdn.net/sinat_32502451/article/details/140155044

public class MySpring {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("classpath*:mySpring.xml");//在 getBean 这一行,打个断点Person person = context.getBean("person", Person.class);System.out.println("=======>"+ person.getName());}}

BeanFactory 的 getBean() 源码:

public interface BeanFactory {//根据 bean 的名称,返回 bean对象.Object getBean(String name) throws BeansException;//根据 bean 名称,以及 匹配bean的类型,返回 bean对象.<T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException;Object getBean(String name, Object... args) throws BeansException;<T> T getBean(Class<T> requiredType) throws BeansException;<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;}	

bean 的作用域:

BeanFactory 中这两个方法,用于判断 bean 的作用域。

	//作用域是否为单例模式?如果是, 每次调用 getBean()方法都会返回同一个对象。boolean isSingleton(String name) throws NoSuchBeanDefinitionException;//作用域是否为原型模式?如果是, 每次调用 getBean()方法都会返回不同的对象。boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

bean 对象的三级缓存:

bean 对象主要放在 map 里面,map 作为缓存。map 的key 是 bean对象的名称, value 是 bean 对象或者对象工厂。

Spring 使用了 三级缓存。

详情见:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry

	/** 第一级缓存,里面放置的是已经实例化好的单例对象 */private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);/** 第二级缓存,里面存放的是提前曝光的单例对象 */private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);/** 第三级缓存,里面存放的是将要被实例化的对象的对象工厂 */private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);

添加 bean 对象到缓存中 :

  • 创建 bean 对象,添加 到缓存的具体方法:

重点看下这几个方法。

org.springframework.context.support.AbstractApplicationContext#refresh

----> org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization

----> org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons

----> org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)

----> org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

----> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)

接下来会 实例化bean :

-----> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])

-----> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

初始化 bean:

-----> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)

添加 bean 对象到缓存:

----> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#addSingleton

获取 bean对象:

----> org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)

----> org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

----> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)

可以看到 实例化bean 、初始化 bean、 获取 bean对象, 都调用了 getBean() 方法。

getBean() 的具体实现:

重点看 getSingleton() 方法 和 createBean() 方法。

	protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {final String beanName = transformedBeanName(name);Object bean;// 检查单例缓存中是否有手动注册的单例Object sharedInstance = getSingleton(beanName);if (sharedInstance != null && args == null) {if (logger.isDebugEnabled()) {if (isSingletonCurrentlyInCreation(beanName)) {logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +"' that is not fully initialized yet - a consequence of a circular reference");}else {logger.debug("Returning cached instance of singleton bean '" + beanName + "'");}}bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);}else {// 如果我们已经在创建这个bean实例,则失败// We're assumably within a circular reference.if (isPrototypeCurrentlyInCreation(beanName)) {throw new BeanCurrentlyInCreationException(beanName);}//检查此工厂中是否存在bean定义BeanFactory parentBeanFactory = getParentBeanFactory();if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {String nameToLookup = originalBeanName(name);if (parentBeanFactory instanceof AbstractBeanFactory) {return ((AbstractBeanFactory) parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly);}else if (args != null) {return (T) parentBeanFactory.getBean(nameToLookup, args);}else {return parentBeanFactory.getBean(nameToLookup, requiredType);}}if (!typeCheckOnly) {markBeanAsCreated(beanName);}try {final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);checkMergedBeanDefinition(mbd, beanName, args);// 保证初始化当前bean所依赖的beanString[] dependsOn = mbd.getDependsOn();if (dependsOn != null) {for (String dep : dependsOn) {if (isDependent(beanName, dep)) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");}registerDependentBean(dep, beanName);try {getBean(dep);}catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"'" + beanName + "' depends on missing bean '" + dep + "'", ex);}}}// 判断是否单例模式if (mbd.isSingleton()) {//获取单例模式的 bean,并添加到缓存中。这里可以重点看看。sharedInstance = getSingleton(beanName, () -> {try {//创建bean实例。。重点看看。return createBean(beanName, mbd, args);}catch (BeansException ex) {// 从单例缓存中删除实例destroySingleton(beanName);throw ex;}});bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);}else if (mbd.isPrototype()) {// 如果是原型模式,就创建一个新的实例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实例的类型匹配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.isDebugEnabled()) {logger.debug("Failed to convert bean '" + name + "' to required type '" +ClassUtils.getQualifiedName(requiredType) + "'", ex);}throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());}}return (T) bean;}

创建 bean 对象:

关键看 实例化 bean 、 初始化 bean 。

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)throws BeanCreationException {BeanWrapper instanceWrapper = null;if (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper == null) {instanceWrapper = createBeanInstance(beanName, mbd, args);}// 实例化 bean。 核心。final Object bean = instanceWrapper.getWrappedInstance();Class<?> beanType = instanceWrapper.getWrappedClass();if (beanType != NullBean.class) {mbd.resolvedTargetType = beanType;}// Allow post-processors to modify the merged bean definition.synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {try {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.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));}Object exposedObject = bean;try {populateBean(beanName, mbd, instanceWrapper);// 初始化bean。。核心。重点看看。exposedObject = initializeBean(beanName, exposedObject, mbd);}catch (Throwable ex) {if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {throw (BeanCreationException) ex;}else {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);}}if (earlySingletonExposure) {Object earlySingletonReference = getSingleton(beanName, false);if (earlySingletonReference != null) {if (exposedObject == bean) {exposedObject = earlySingletonReference;}else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {String[] dependentBeans = getDependentBeans(beanName);Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);for (String dependentBean : dependentBeans) {if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean);}}if (!actualDependentBeans.isEmpty()) {throw new BeanCurrentlyInCreationException(beanName,"Bean with name '" + beanName + "' has been injected into other beans [" +StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +"] in its raw version as part of a circular reference, but has eventually been " +"wrapped. This means that said other beans do not use the final version of the " +"bean. This is often the result of over-eager type matching - consider using " +"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");}}}}// Register bean as disposable.try {registerDisposableBeanIfNecessary(beanName, bean, mbd);}catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);}return exposedObject;}

初始化bean:

	protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareMethods(beanName, bean);return null;}, getAccessControlContext());}else {invokeAwareMethods(beanName, bean);}Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {invokeInitMethods(beanName, wrappedBean, mbd);}catch (Throwable ex) {throw new BeanCreationException((mbd != null ? mbd.getResourceDescription() : null),beanName, "Invocation of init method failed", ex);}if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;}
  • 获取单例对象:

根据 bean 名称,返回已经注册的单例对象,

如果没有注册,则创建并注册一个新对象。

/***   根据 bean 名称,返回已经注册的单例对象,如果没有注册,则创建并注册一个新对象。*/ 
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {Assert.notNull(beanName, "Bean name must not be null");//加同步锁synchronized (this.singletonObjects) {//根据 bean 名称,返回已经注册的单例对象Object singletonObject = this.singletonObjects.get(beanName);if (singletonObject == null) {if (this.singletonsCurrentlyInDestruction) {throw new BeanCreationNotAllowedException(beanName,"Singleton bean creation not allowed while singletons of this factory are in destruction " +"(Do not request a bean from a BeanFactory in a destroy method implementation!)");}if (logger.isDebugEnabled()) {logger.debug("Creating shared instance of singleton bean '" + beanName + "'");}beforeSingletonCreation(beanName);boolean newSingleton = false;boolean recordSuppressedExceptions = (this.suppressedExceptions == null);if (recordSuppressedExceptions) {this.suppressedExceptions = new LinkedHashSet<>();}try {singletonObject = singletonFactory.getObject();newSingleton = true;}catch (IllegalStateException ex) {singletonObject = this.singletonObjects.get(beanName);if (singletonObject == null) {throw ex;}}catch (BeanCreationException ex) {if (recordSuppressedExceptions) {for (Exception suppressedException : this.suppressedExceptions) {ex.addRelatedCause(suppressedException);}}throw ex;}finally {if (recordSuppressedExceptions) {this.suppressedExceptions = null;}afterSingletonCreation(beanName);}//这一块可以重点看下。 判断是否新的单例对象。if (newSingleton) {//前面介绍过, 如果是新的单例对象,此处将 bean 添加到 缓存中addSingleton(beanName, singletonObject);}}return singletonObject;}
}
  • addSingleton() 方法:

如下, 将 bean 添加到 缓存中 :

	protected void addSingleton(String beanName, Object singletonObject) {synchronized (this.singletonObjects) {//添加到缓存中。beanName值类似 上面的对象名"person" //singletonObject类似具体的对象 Person(age=1, name=Tom, fullName=null, dateOfBirth=null)this.singletonObjects.put(beanName, singletonObject);this.singletonFactories.remove(beanName);this.earlySingletonObjects.remove(beanName);this.registeredSingletons.add(beanName);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/46129.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

输入法发展历史

输入法的发展历史&#xff0c;尤其是中文输入法&#xff0c;是一个相当丰富和多元的话题&#xff0c;它反映了技术进步、用户需求变化以及计算机和移动设备界面设计的演进。以下是一个概览&#xff1a; 早期阶段 1970s&#xff1a;朱邦复在1976年发明了仓颉输入法&#xff0c;…

python:绘制一元四次函数的曲线

编写 test_x4_x2_4x.py 如下 # -*- coding: utf-8 -*- """ 绘制函数 y x^4x^24x-3 在 -2<x<2 的曲线 """ import numpy as np from matplotlib import pyplot as plt# 用于正常显示中文标题&#xff0c;负号 plt.rcParams[font.sans-s…

Amazon EC2 部署Ollama + webUI

最近和同事闲聊&#xff0c;我们能不能内网自己部署一个LLM&#xff0c;于是便有了Ollama webUI的尝试 对于Linux&#xff0c;使用一行命令即可 curl -fsSL https://ollama.com/install.sh | shollama --help Large language model runnerUsage:ollam…

网络规划设计师教程(第二版) pdf

网络规划设计师教程在网上找了很多都是第一版&#xff0c;没有第二版。 所以去淘宝买了第二版的pdf&#xff0c;与其自己独享不如共享出来&#xff0c;让大家也能看到。 而且这个pdf我已经用WPS扫描件识别过了&#xff0c;可以直接CtrlF搜索关键词&#xff0c;方便查阅。 链接…

element-ui 表格固定头和固定列表格错位

问题&#xff1a;当 el-table 组件固定了表头和列时&#xff0c;列表数据触发滚动条&#xff0c;列表滚动到底&#xff0c;会有错位。 原因分析&#xff1a;因为固定列和固定表头产生的滚动条宽度不一致&#xff0c;导致有轻微误差。 解决&#xff1a;自定义滚动条高度&#…

PostgreSQL 中如何解决因频繁的小事务导致的性能下降?

&#x1f345;关注博主&#x1f397;️ 带你畅游技术世界&#xff0c;不错过每一次成长机会&#xff01;&#x1f4da;领书&#xff1a;PostgreSQL 入门到精通.pdf 文章目录 PostgreSQL 中解决因频繁小事务导致性能下降的方法 PostgreSQL 中解决因频繁小事务导致性能下降的方法…

搭建调用链监控Zipkin和Sleuth

项目环境: win7、jdk8 1、添加依赖,添加了spring-cloud-starter-zipkin会自动导入Sleuth <!--Sleuth,zipkin--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId><ve…

nodejs实现数据库备份、清空表、删除表

备份数据库 使用 mysqldump 库&#xff0c;实现数据备份 var path require(path) var fs require(fs) const mysqldump require(mysqldump)const backupsMysql () > {const backupFolder path.join(__dirname, backup)if (!fs.existsSync(backupFolder)) {fs.mkdirSync…

基于SpringBoot的校园志愿者管理系统

你好呀&#xff0c;我是计算机学姐码农小野&#xff01;如果有相关需求&#xff0c;可以私信联系我。 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringBoot框架 工具&#xff1a;MyEclipse、Tomcat 系统展示 首页 个人中心 志愿者管理 活动信息…

Python + Playwright(20):模拟手机设备

Python + Playwright(20):模拟手机设备 前言为什么要模拟手机设备?使用Playwright模拟手机设备(基础示例)使用Playwright模拟手机设备(复杂示例)进阶配置和技巧自定义设备配置:模拟特定的屏幕尺寸移动设备性能测试测量页面加载时间模拟不同的网络条件总结前言 在UI自动…

【每日一练】python函数与装饰器

Python装饰器是一种用于修改函数或类的行为的语法结构。装饰器可以在不修改原始函数或类的情况下&#xff0c;为它们添加额外的功能。 使用装饰器的常见场景是在不改变原函数代码的情况下&#xff0c;给函数添加日志记录、性能统计、输入验证等功能。装饰器还可以用于授权、缓…

SQL性能下降的原因

一、SQL性能下降的原因 主要是性能下降SQL慢、执行时间长、等待时间长 不是一条SQL抓出来就要优化&#xff0c;在真实的生产环境下这种故障第一个要去复线&#xff0c;有可能去排查的时候没&#xff0c;所以没法复线。 可能需要它跑半天或者一天来缩小筛查的范围&#xff0c…

ThingsBoard本地windows环境启动

提示&#xff1a;以下内容只是本人自己在环境运行情况&#xff0c;不同环境或许会有区别请注意甄别 本地部署 前言简介正文实现总结 前言 前置环境的配置&#xff0c;首先电脑需要配置了git、maven、JDK17 本人使用的maven版本号为3.8.4 软件版本号git2.42.0maven3.8.4jdk17n…

three-tile开发: 5. 取得地图的地面信息

three-tile 是一个开源的轻量级三维瓦片库&#xff0c;它基于threejs使用typescript开发&#xff0c;提供一个三维地形模型&#xff0c;能轻松给你的应用增加三维瓦片地图。 项目地址&#xff1a;GitHub - sxguojf/three-tile: 3D tile map using threejs 示例地址&#xff1a;…

JVM:自动垃圾回收

文章目录 一、C/C的内存管理二、Java的内存管理1、方法去的回收2、堆回收&#xff08;1&#xff09;引用计数法和可达性分析法&#xff08;2&#xff09;五种对象引用&#xff08;3&#xff09;垃圾回收算法&#xff08;4&#xff09;垃圾回收器 一、C/C的内存管理 在C和C没有…

uni-app 蓝牙传输

https://www.cnblogs.com/ckfuture/p/16450418.html https://www.cnblogs.com/yangxiaobai123/p/16021058.html 字符串转base64&#xff1a;https://www.cnblogs.com/sunny3158/p/17312158.html 将 ArrayBuffer 对象转成 Base64 字符串&#xff1a;基础 - uni.arrayBufferT…

IT运维也有自己的节日 724向日葵IT运维节,三大版本如何选?

“724运维节”&#xff0c;是2016年由开放运维联盟发起倡议&#xff0c;广大运维人员共同投票产生的属于运维人自己的节日。 对于运维人最大的印象&#xff0c;那就是工作都需要7x24小时待命&#xff0c;是名副其实的“日不落骑士”&#xff0c;这也是大家选择724这一天作为运…

原理图大结局

一、总结哪些地方是5V供电&#xff1f;哪些地方是4V供电&#xff1f;哪些地方是3.3V供电&#xff1f;为什么会这样&#xff1f;根据什么原则来划分供电区域&#xff1f; 二、 5V 供电为什么有的地方要100uF&#xff0b; 0.1uF 滤波&#xff1f;有的地方只要 10uF 滤波&#xff…

RabbitMQ - 延迟消息 - 死信交换机

目录 1、怎么理解延迟消息&#xff1f; 2、如何实现延迟消息&#xff1f; 2.1、方案一&#xff1a;死信交换机 2.1.1、什么是死信&#xff1a; 2.1.2、什么是死信交换机&#xff1f; 2.2、方案二&#xff1a;延迟消息插件 2.2.1、插件安装&#xff1a; 2.2.2、代码实现 …

AndroidStudio2023.3版本avd manager模拟器无法创建

创建到最后一步的时候提示WARN - #com.android.sdklib.internal.avd.AvdManager - com.android.prefs.AndroidLocationsException: Can’t locate Android SDK installation directory for the AVD .ini file. 前提&#xff1a; 1.sdk路径没问题 2.安装了下图内容 那是什么原因…