Sping源码(六)— prepareBeanFactory()
前言
前几篇已经介绍lBeanFactory的创建已经xml配置文件中自定义标签和默认标签的解析过程,此时的BeanFactory中已经包含了xml文件中的标签属性。但BeanFactory中还有一些本身重要的属性没有填充,所以接着方法继续往下看BeanFactory中的属性是如何填充的。
refresh
refresh()主流程中前几个方法已经介绍过,这里不过多赘述,去掉后面的无用代码,我们这篇帖子主要看prepareBeanFactory方法中做了些什么。
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing./*** 前戏,做容器刷新前的准备工作* 1、设置容器的启动时间* 2、设置活跃状态为true* 3、设置关闭状态为false* 4、获取Environment对象,并加载当前系统的属性值到Environment对象中* 5、准备监听器和事件的集合对象,默认为空的集合*/prepareRefresh();// Tell the subclass to refresh the internal bean factory.// 创建容器对象:DefaultListableBeanFactory// 加载xml配置文件的属性值到当前工厂中,最重要的就是BeanDefinitionConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.// beanFactory的准备工作,对各种属性进行填充prepareBeanFactory(beanFactory);}}
prepareBeanFactory
见名知意,这个方法中也是为了BeanFactory做准备工作,所以刚上来设置了BeanFacroty的类加载器。
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {// Tell the internal bean factory to use the context's class loader etc.// 设置beanFactory的classloader为当前context的classloaderbeanFactory.setBeanClassLoader(getClassLoader());// 设置beanfactory的表达式语言处理器beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));// 为beanFactory增加一个默认的propertyEditor,这个主要是对bean的属性等设置管理的一个工具类beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));// Configure the bean factory with context callbacks.// 添加beanPostProcessor,ApplicationContextAwareProcessor此类用来完成某些Aware对象的注入beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));// 设置要忽略自动装配的接口,这些接口的实现是由容器通过set方法进行注入的,// 所以在使用autowire进行注入的时候需要将这些接口进行忽略beanFactory.ignoreDependencyInterface(EnvironmentAware.class);beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);beanFactory.ignoreDependencyInterface(MessageSourceAware.class);beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);// BeanFactory interface not registered as resolvable type in a plain factory.// MessageSource registered (and found for autowiring) as a bean.// 设置几个自动装配的特殊规则,当在进行ioc初始化的如果有多个实现,那么就使用指定的对象进行注入beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);beanFactory.registerResolvableDependency(ResourceLoader.class, this);beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);beanFactory.registerResolvableDependency(ApplicationContext.class, this);// Register early post-processor for detecting inner beans as ApplicationListeners.// 注册BPPbeanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));// Detect a LoadTimeWeaver and prepare for weaving, if found.// 增加对AspectJ的支持,在java中织入分为三种方式,分为编译器织入,类加载器织入,运行期织入,编译器织入是指在java编译器,采用特殊的编译器,将切面织入到java类中,// 而类加载期织入则指通过特殊的类加载器,在类字节码加载到JVM时,织入切面,运行期织入则是采用cglib和jdk进行切面的织入// aspectj提供了两种织入方式,第一种是通过特殊编译器,在编译器,将aspectj语言编写的切面类织入到java类中,第二种是类加载期织入,就是下面的load time weaving,此处后续讲if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));// Set a temporary ClassLoader for type matching.beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));}// Register default environment beans.// 注册默认的系统环境bean到一级缓存中if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());}if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());}if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());}}
StandardBeanExpressionResolver
这个方法主要是创建了一个标准的Bean表达式的解析器。主要是用来对spel表达式的解析。
其中类之间的关系是Resolver处理类 包含了-> spelParser的解析类 包含了 -> spelConfiguration配置类。其中具体的解析过程是在SpelExpressionParser中的doParseExpression()方法完成。
/*** 默认表达式前缀** Default expression prefix: "#{". */public static final String DEFAULT_EXPRESSION_PREFIX = "#{";/*** 默认表达式后缀** Default expression suffix: "}". */public static final String DEFAULT_EXPRESSION_SUFFIX = "}";private String expressionPrefix = DEFAULT_EXPRESSION_PREFIX;private String expressionSuffix = DEFAULT_EXPRESSION_SUFFIX;public StandardBeanExpressionResolver(@Nullable ClassLoader beanClassLoader) {this.expressionParser = new SpelExpressionParser(new SpelParserConfiguration(null, beanClassLoader));}public SpelParserConfiguration(@Nullable SpelCompilerMode compilerMode, @Nullable ClassLoader compilerClassLoader,boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {this.compilerMode = (compilerMode != null ? compilerMode : defaultCompilerMode);this.compilerClassLoader = compilerClassLoader;this.autoGrowNullReferences = autoGrowNullReferences;this.autoGrowCollections = autoGrowCollections;this.maximumAutoGrowSize = maximumAutoGrowSize;}
SpelExpressionParser
省略具体解析过程。知道使用时会调用doParseExpression方法即可。
public SpelExpressionParser(SpelParserConfiguration configuration) {Assert.notNull(configuration, "SpelParserConfiguration must not be null");this.configuration = configuration;}@Overrideprotected SpelExpression doParseExpression(String expressionString, @Nullable ParserContext context) throws ParseException {return new InternalSpelExpressionParser(this.configuration).doParseExpression(expressionString, context);}
回到prepareBeanFactory主方法,看下面的具体方法。
addPropertyEditorRegistrar
这个方法主要是对类属性的一个扩展,比如说Customer类中引用了Address变量。Address中包含了省市区的属性,但Customer中对address的值是省_市_区,将值捏合成了一个,这时可通过自定义的扩展对类中属性进行拆分。
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {// Tell the internal bean factory to use the context's class loader etc.// 设置beanFactory的classloader为当前context的classloaderbeanFactory.setBeanClassLoader(getClassLoader());// 设置beanfactory的表达式语言处理器beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));// 为beanFactory增加一个默认的propertyEditor,这个主要是对bean的属性等设置管理的一个工具类beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));}
先看方法的具体执行流程。
ResourceEditorRegistrar
addPropertyEditorRegistrar()方法会为BeanFacroty中PropertyEditorRegistrar属性赋值,在PropertyEditorRegistrar的构造器中,会对类中属性进行初始化赋值,并在类中包含了一个registerCustomEditors()方法。
registerCustomEditors方法会将Spring中默认的一些类注册到Editors中,默认的会执行overrideDefaultEditor操作。
registerCustomEditors方法会在Spring后面的源码中进行调用,这里不过多讲解。
public class ResourceEditorRegistrar implements PropertyEditorRegistrar {public ResourceEditorRegistrar(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {this.resourceLoader = resourceLoader;this.propertyResolver = propertyResolver;}@Overridepublic void registerCustomEditors(PropertyEditorRegistry registry) {ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);doRegisterEditor(registry, Resource.class, baseEditor);//省略部分doRegisterEditor代码...if (this.resourceLoader instanceof ResourcePatternResolver) {doRegisterEditor(registry, Resource[].class,new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));}}private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {if (registry instanceof PropertyEditorRegistrySupport) {((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);}else {registry.registerCustomEditor(requiredType, editor);}}
}
ResourceEditor
ResourceEditor类继承了PropertyEditorSupport,PropertyEditorSupport实现了PropertyEditor类,其中ResourceEditor类包含了一些对类属性的一些实现。包括setAsText,setValue等一些方法。
public class ResourceEditor extends PropertyEditorSupport {public void setAsText(String text) throws java.lang.IllegalArgumentException {if (value instanceof String) {setValue(text);return;}throw new java.lang.IllegalArgumentException(text);}public void setValue(Object value) {this.value = value;firePropertyChange();}
}
结合ResourceEditor和ResourceEditorRegistrar两者来看的话,对属性的扩展就是将具体的class类,通过Editor定义好具体的逻辑后,通过Spring框架的识别,而后根据自己定义的逻辑来针对具体的class类的属性进行处理。
自定义类属性扩展
根据上面介绍的执行流程,仿照Spring框架的写法:
- 需要类实现PropertyEditorRegistrar类并实现自己的registerCustomEditors方法将类注册到Editors类中
- 在自定义的Editors类中,重写setAsText方法,实现类中的属性自定义扩展。
- 让自定义的Registrar类能够被Spring发现,并调用。
自定义类
Customer类中包含着Address属性,此时Customer对address的属性设置是“”省_市_区“”的格式,到Address类中要拆分成对应的值。
public class Customer {private String name;private Address address;
}public class Address {private String province;private String city;private String town;
}
实现Registrar
实现Registrar,调实现registerCustomEditors注册。
public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar {@Overridepublic void registerCustomEditors(PropertyEditorRegistry registry) {registry.registerCustomEditor(Address.class,new AddressPropertyEditor());}
}
//根据参数text属性实现自定义扩展
public class AddressPropertyEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text) throws IllegalArgumentException {String[] s = text.split("_");Address address = new Address();address.setProvince(s[0]);address.setCity(s[1]);address.setTown(s[2]);this.setValue(address);}
}
xml配置
这里的xml采用了两种方法配置,目的是让Spring框架可以发现我们自定义的Editor,并进行调用。将我们自定义的Editor填充到CustomEditorConfigurer类中的customEditors属性其实就可让Spring框架发现并调用。这里埋个小坑,会在后面源码具体执行调用customEditors属性时再填。
<bean id="customer" class="com.xxx.selfEditor.Customer"><property name="name" value="zhangsan"></property><property name="address" value="省_市_区"></property></bean>
<!-- <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">-->
<!-- <property name="propertyEditorRegistrars">-->
<!-- <list>-->
<!-- <bean class="com.xxx.selfEditor.AddressPropertyEditorRegistrar"></bean>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean>--><bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors"><map><entry key="com.xxx.selfEditor.Address"><value>com.xxx.selfEditor.AddressPropertyEditor</value></entry></map></property></bean>
ignoreDependencyInterface
忽略自动装配的接口,这些接口的实现是由容器通过set方法进行注入的,所以在使用autowire进行注入的时候需要将这些接口进行忽略。
源码中忽略类的方法有两个,代表着不同的含义,具体可以看这篇帖子,关于ignoreDependencyInterface的方法讲述的很细。
/*** 自动装配时忽略的类** Ignore the given dependency type for autowiring:* for example, String. Default is none.* @param type the dependency type to ignore*/void ignoreDependencyType(Class<?> type);/*** 自动装配时忽略的接口** Ignore the given dependency interface for autowiring.* <p>This will typically be used by application contexts to register* dependencies that are resolved in other ways, like BeanFactory through* BeanFactoryAware or ApplicationContext through ApplicationContextAware.* <p>By default, only the BeanFactoryAware interface is ignored.* For further types to ignore, invoke this method for each type.* @param ifc the dependency interface to ignore* @see org.springframework.beans.factory.BeanFactoryAware* see org.springframework.context.ApplicationContextAware*/void ignoreDependencyInterface(Class<?> ifc);
相关文章:

Sping源码(六)— prepareBeanFactory()
前言 前几篇已经介绍lBeanFactory的创建已经xml配置文件中自定义标签和默认标签的解析过程,此时的BeanFactory中已经包含了xml文件中的标签属性。但BeanFactory中还有一些本身重要的属性没有填充,所以接着方法继续往下看BeanFactory中的属性是如何填充的…...

LeetCode(力扣)257. 二叉树的所有路径Python
LeetCode257. 二叉树的所有路径 题目链接代码 题目链接 https://leetcode.cn/problems/binary-tree-paths/ 代码 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val0, leftNone, rightNone): # self.val val # self.…...

nodejs实现http与https服务;同时处理proxy代理的解决方案
// nodejs服务提供的http协议示例 const http require(http); const server http.createServer((req, res) > {res.writeHead(200, { Content-Type: text/html;charsetutf8 });res.end(Date.now() > http访问成功8080) }); server.listen(8080, () > {console.lo…...

C# WPF ListBox 动态显示图片
前言 最近在和其他软件联合做一个本地图片选择传输功能,为此希望图片能够有序的呈现在客户端,简单的实现了一下功能,通过Mvvm模式进行呈现,过程简单通俗,话不多说直接上图。 处理过程 前台代码 你只需要粘贴到你的前台…...

游戏如何防御DDOS流量攻击呢,用游戏盾真的有用么?
针对在线游戏行业来说,DDoS(分布式拒绝服务)攻击是一种极具破坏性的威胁。DDoS攻击可能导致游戏服务器不可用,严重影响游戏体验和运营。为了解决这一问题,游戏盾作为一种专门为游戏行业设计的安全解决方案,…...

vue项目引入antDesignUI组件
快速安装ant-design-vue并配置,vue2.0 antDesign1.7.8 第一步:安装ant-deisgn-vue 1.7.8 npm install ant-design-vue1.7.8 --save第二步:配置package.json文件,将依赖写入后,npm install 安装依赖 "dependenc…...

非结构化数据库-MinIO基本集成
是什么 MinIO 是一个高性能的分布式对象存储服务,适合存储非结构化数据,如图片,音频,视频,日志等。对象文件最大可以达到5TB。 安装启动 mkdir -p /usr/local/minio cd /usr/local/minio# 下载安装包 wget https:/…...

Etcd备份及恢复
一、Etcd数据备份 1、备份命令 [rootlocalhost ~]# export ETCDCTL_API3 [rootlocalhost ~]# /data/etcd-3.4.9/bin/etcdctl --endpoints10.2.20.108:2379 snapshot save etcd-date "%Y-%m-%d_%H-%M-%S".snapshot 2、备份完成后会在当前目录生成备份文件 [rootlo…...

使用JavaMail发送邮件时嵌入公司logo图片
使用JavaMail发送邮件时嵌入公司logo图片 第一种方式:img标签和logo图片链接第二种方式:使用img标签和图片base64字符串第三种方式(推荐):将logo当做附件一起发送并设置ContentID,再使用img标签,…...

注解 @Async
注解 Async 1. 注解由来: Async 是 Spring 框架提供的注解,用于将方法标记为异步执行。通过使用 Async 注解,可以告知 Spring 在调用被注解的方法时,使用新的线程或线程池进行异步执行。 2. 注解示例: import org.s…...

Python“牵手”lazada商品评论数据采集方法,lazadaAPI申请指南
lazada平台API接口是为开发电商类应用程序而设计的一套完整的、跨浏览器、跨平台的接口规范,lazadaAPI接口是指通过编程的方式,让开发者能够通过HTTP协议直接访问lazada平台的数据,包括商品信息、店铺信息、物流信息等,从而实现la…...

微信小程序通用字体代码
下面是一个简单的微信小程序通用字体代码示例: // 在app.wxss中设置全局字体样式 import ./styles/fonts.wxss;// 在fonts.wxss中定义字体样式 font-face {font-family: CustomFont;src: url(font.ttf) format(truetype); }// 在page.wxss中使用自定义字体样式 .cus…...

LVS负载均衡DR模式
在LVS(Linux Virtual Server)负载均衡中的DR(Direct Routing)模式下,数据包的流向如下: 客户端发送请求到负载均衡器(LVS)的虚拟IP(VIP)。负载均衡器&#x…...

ArcGIS Pro基础入门、制图、空间分析、影像分析、三维建模、空间统计分析与建模、python融合、案例全流程科研能力提升
目录 第一章 入门篇 GIS理论及ArcGIS Pro基础 第二章 基础篇 ArcGIS数据管理与转换 第三章 数据编辑与查询、拓扑检查 第四章 制图篇 地图符号与版面设计 第五章 空间分析篇 ArcGIS矢量空间分析及应用 第六章 ArcGIS栅格空间分析及应用 第七章 影像篇 遥感影像处理 第八…...

Spring Clould 配置中心 - Nacos
视频地址:微服务(SpringCloudRabbitMQDockerRedis搜索分布式) Nacos配置管理-Nacos实现配置管理(P24、P25) Nacos除了可以做注册中心,同样可以做配置管理来使用。 当微服务部署的实例越来越多,…...

1609.奇偶数
目录 一、题目 二、代码 三、完整测试代码 一、题目 1609. 奇偶树 - 力扣(LeetCode) 二、代码 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0),…...

c++--异常
1.什么是异常 对于C语言来说,处理错误的机制有: 1.终止程序:如assert,缺陷,如发生内存错误,除0之外发生程序终止,用户无法接受。 2.返回错误码:对于大型程序来说,需要…...

ArcGIS 利用cartogram插件制作变形地图
成果图 注:本图数据并不完全对,只做为测试用例 操作 首先需要下载一个插件cartogram 下载地址在这里 https://www.arcgis.com/home/item.html?idd348614c97264ae19b0311019a5f2276 下载完毕之后解压将Cartograms\HelpFiles下的所有文件复制到ArcGIS…...

Mybatis批量插入方式有哪些
MyBatis的批量插入有多种写法,下面我将列出一些常见的批量插入写法 方式列表 使用XML配置文件进行批量插入:在XML映射文件中使用<insert>标签,并通过foreach标签迭代批量数据,然后在SQL语句中使用VALUES关键字。使用Java注…...

前端框架学习-React(一)
React 应用程序是由组件组成的。 react 程序是用的jsx语法,使用这种语法的代码需要由babel进行解析,解析成js代码。 jsx语法: 只能返回一个根元素 所有的标签都必须闭合(自闭和或使用一对标签的方式闭合) 使用驼峰式…...

Android Studio实现解析HTML获取图片URL将图片保存到本地
目录 效果activity_main.xmlMainActivityImageItemImageAdapter 效果 项目本来是要做成图片保存到手机然后读取数据后瀑布流展示,但是有问题,目前只能做到保存到手机 activity_main.xml <?xml version"1.0" encoding"utf-8"?…...

单例模式的理论与实践
本文实践代码仓库:https://github.com/goSilver/my_practice 文章目录 一、定义二、作用三、实现3.1 饿汉式3.2 懒汉式3.3 双重检查3.4 静态内部类3.5 枚举 四、总结4.1 单例存在哪些问题?4.2 单例有什么替代解决方案? 一、定义 单例模式是一…...

深入了解MongoDB:灵活的文档型数据库与应用案例
什么是MongoDB ? MongoDB 是由C语言编写的,是一个基于分布式文件存储的开源数据库系统。 在高负载的情况下,添加更多的节点,可以保证服务器性能。 MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB 将数据存储为一个…...

【HarmonyOS北向开发】-01 HarmonyOS概述
飞书原文链接-【HarmonyOS北向开发】-01 HarmonyOS概述https://fvcs2dhq8qs.feishu.cn/docx/TDf2d2KMaoPSUUxnvg2cASDdnCe?fromfrom_copylink...

Node.js入门
安装 前往官网下载即可:https://nodejs.org/zh-cn 安装之后检查是否成功并查看版本,winr --> 输入cmd --> 确认 --> 进入命令提示符窗口 --> 输入 node -v --> 出现以下就代表成功了,这也是node的版本号 什么是Node.js Nod…...

指针、数组、sizeof、strlen相关知识与练习题目
目录 前提回顾🔍: 关于一维数组🤮: 关于二维数组😀: sizeof与strlen🐕: sizeof🏀: strlen🐓: 相关练习📚:…...

分类预测 | MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测
分类预测 | MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测 目录 分类预测 | MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测,运行环境Matlab2023b及以上…...

MyBatis动态SQL:打造灵活可变的数据库操作
目录 if标签trim标签where标签set标签foreach标签 动态SQL就是根据不同的条件或需求动态地生成查询语句,比如动态搜索条件、动态表或列名、动态排序等。 if标签 在我们填写一些信息时,有些信息是必填字段,有的则是非必填的,这些…...

nginx代理请求到内网不同服务器
需求:之前用的是frp做的内网穿透,但是每次电脑断电重启,路由或者端口会冲突,现在使用汉土云盒替换frp。 需要把公网ip映射到任意一台内网服务器上,然后在这台内网服务器上用Nginx做代理即可访问内网其它服务器…...

【C# 基础精讲】文件读取和写入
文件读取和写入是计算机程序中常见的操作,用于从文件中读取数据或将数据写入文件。在C#中,使用System.IO命名空间中的类来进行文件读写操作。本文将详细介绍如何在C#中进行文件读取和写入,包括读取文本文件、写入文本文件、读取二进制文件和写…...