做网站论坛/网站排名优化软件
序
本文主要研究一下springboot的ConfigurationProperties的绑定
ConfigurationPropertiesBindingPostProcessor
org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java
/*** {@link BeanPostProcessor} to bind {@link PropertySources} to beans annotated with* {@link ConfigurationProperties @ConfigurationProperties}.** @author Dave Syer* @author Phillip Webb* @author Christian Dupuis* @author Stephane Nicoll* @author Madhura Bhave* @since 1.0.0*/
public class ConfigurationPropertiesBindingPostProcessorimplements BeanPostProcessor, PriorityOrdered, ApplicationContextAware, InitializingBean {/*** The bean name that this post-processor is registered with.*/public static final String BEAN_NAME = ConfigurationPropertiesBindingPostProcessor.class.getName();private ApplicationContext applicationContext;private BeanDefinitionRegistry registry;private ConfigurationPropertiesBinder binder;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}@Overridepublic void afterPropertiesSet() throws Exception {// We can't use constructor injection of the application context because// it causes eager factory bean initializationthis.registry = (BeanDefinitionRegistry) this.applicationContext.getAutowireCapableBeanFactory();this.binder = ConfigurationPropertiesBinder.get(this.applicationContext);}@Overridepublic int getOrder() {return Ordered.HIGHEST_PRECEDENCE + 1;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {bind(ConfigurationPropertiesBean.get(this.applicationContext, bean, beanName));return bean;}private void bind(ConfigurationPropertiesBean bean) {if (bean == null || hasBoundValueObject(bean.getName())) {return;}Assert.state(bean.getBindMethod() == BindMethod.JAVA_BEAN, "Cannot bind @ConfigurationProperties for bean '"+ bean.getName() + "'. Ensure that @ConstructorBinding has not been applied to regular bean");try {this.binder.bind(bean);}catch (Exception ex) {throw new ConfigurationPropertiesBindException(bean, ex);}}private boolean hasBoundValueObject(String beanName) {return this.registry.containsBeanDefinition(beanName) && this.registry.getBeanDefinition(beanName) instanceof ConfigurationPropertiesValueObjectBeanDefinition;}/*** Register a {@link ConfigurationPropertiesBindingPostProcessor} bean if one is not* already registered.* @param registry the bean definition registry* @since 2.2.0*/public static void register(BeanDefinitionRegistry registry) {Assert.notNull(registry, "Registry must not be null");if (!registry.containsBeanDefinition(BEAN_NAME)) {BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(ConfigurationPropertiesBindingPostProcessor.class,ConfigurationPropertiesBindingPostProcessor::new).getBeanDefinition();definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);registry.registerBeanDefinition(BEAN_NAME, definition);}ConfigurationPropertiesBinder.register(registry);}}
ConfigurationPropertiesBindingPostProcessor实现了BeanPostProcessor、PriorityOrdered、ApplicationContextAware、InitializingBean四个接口;其getOrder方法返回的是
Ordered.HIGHEST_PRECEDENCE + 1
即仅次于最高的优先级;其postProcessBeforeInitialization方法主要是执行bind方法(先通过ConfigurationPropertiesBean.get获取ConfigurationPropertiesBean,再通过binder进行bind
);其afterPropertiesSet主要是获取BeanDefinitionRegistry与ConfigurationPropertiesBinder
ConfigurationPropertiesBean.get
org/springframework/boot/context/properties/ConfigurationPropertiesBean.java
public static ConfigurationPropertiesBean get(ApplicationContext applicationContext, Object bean, String beanName) {Method factoryMethod = findFactoryMethod(applicationContext, beanName);return create(beanName, bean, bean.getClass(), factoryMethod);}private static ConfigurationPropertiesBean create(String name, Object instance, Class<?> type, Method factory) {ConfigurationProperties annotation = findAnnotation(instance, type, factory, ConfigurationProperties.class);if (annotation == null) {return null;}Validated validated = findAnnotation(instance, type, factory, Validated.class);Annotation[] annotations = (validated != null) ? new Annotation[] { annotation, validated }: new Annotation[] { annotation };ResolvableType bindType = (factory != null) ? ResolvableType.forMethodReturnType(factory): ResolvableType.forClass(type);Bindable<Object> bindTarget = Bindable.of(bindType).withAnnotations(annotations);if (instance != null) {bindTarget = bindTarget.withExistingValue(instance);}return new ConfigurationPropertiesBean(name, instance, annotation, bindTarget);}
get方法主要是获取工厂方法,之后获取annotation,获取bindTarget,最后创建ConfigurationPropertiesBean
ConfigurationPropertiesBean
org/springframework/boot/context/properties/ConfigurationPropertiesBean.java
/*** Provides access to {@link ConfigurationProperties @ConfigurationProperties} bean* details, regardless of if the annotation was used directly or on a {@link Bean @Bean}* factory method. This class can be used to access {@link #getAll(ApplicationContext)* all} configuration properties beans in an ApplicationContext, or* {@link #get(ApplicationContext, Object, String) individual beans} on a case-by-case* basis (for example, in a {@link BeanPostProcessor}).** @author Phillip Webb* @since 2.2.0* @see #getAll(ApplicationContext)* @see #get(ApplicationContext, Object, String)*/
public final class ConfigurationPropertiesBean {private final String name;private final Object instance;private final ConfigurationProperties annotation;private final Bindable<?> bindTarget;private final BindMethod bindMethod;//......
}
ConfigurationPropertiesBean用于代表一个标注了
@ConfigurationProperties
注解的bean的信息
ConfigurationPropertiesBinder
org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java
/*** Internal class used by the {@link ConfigurationPropertiesBindingPostProcessor} to* handle the actual {@link ConfigurationProperties @ConfigurationProperties} binding.** @author Stephane Nicoll* @author Phillip Webb*/
class ConfigurationPropertiesBinder {private static final String BEAN_NAME = "org.springframework.boot.context.internalConfigurationPropertiesBinder";private static final String FACTORY_BEAN_NAME = "org.springframework.boot.context.internalConfigurationPropertiesBinderFactory";private static final String VALIDATOR_BEAN_NAME = EnableConfigurationProperties.VALIDATOR_BEAN_NAME;private final ApplicationContext applicationContext;private final PropertySources propertySources;private final Validator configurationPropertiesValidator;private final boolean jsr303Present;private volatile Validator jsr303Validator;private volatile Binder binder;ConfigurationPropertiesBinder(ApplicationContext applicationContext) {this.applicationContext = applicationContext;this.propertySources = new PropertySourcesDeducer(applicationContext).getPropertySources();this.configurationPropertiesValidator = getConfigurationPropertiesValidator(applicationContext);this.jsr303Present = ConfigurationPropertiesJsr303Validator.isJsr303Present(applicationContext);}BindResult<?> bind(ConfigurationPropertiesBean propertiesBean) {Bindable<?> target = propertiesBean.asBindTarget();ConfigurationProperties annotation = propertiesBean.getAnnotation();BindHandler bindHandler = getBindHandler(target, annotation);return getBinder().bind(annotation.prefix(), target, bindHandler);}private Binder getBinder() {if (this.binder == null) {this.binder = new Binder(getConfigurationPropertySources(), getPropertySourcesPlaceholdersResolver(),getConversionService(), getPropertyEditorInitializer(), null,ConfigurationPropertiesBindConstructorProvider.INSTANCE);}return this.binder;}//......
}
ConfigurationPropertiesBinder的bind方法根据ConfigurationPropertiesBean的target与annotation取获取bindHandler,然后通过binder去执行bind方法
binder的构造器依赖了propertySources、placeholdersResolver、conversionService、propertyEditorInitializer、defaultBindHandler、constructorProvider
Binder
org/springframework/boot/context/properties/bind/Binder.java
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler,Context context, boolean allowRecursiveBinding) {ConfigurationProperty property = findProperty(name, context);if (property == null && context.depth != 0 && containsNoDescendantOf(context.getSources(), name)) {return null;}AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);if (aggregateBinder != null) {return bindAggregate(name, target, handler, context, aggregateBinder);}if (property != null) {try {return bindProperty(target, context, property);}catch (ConverterNotFoundException ex) {// We might still be able to bind it using the recursive bindersObject instance = bindDataObject(name, target, handler, context, allowRecursiveBinding);if (instance != null) {return instance;}throw ex;}}return bindDataObject(name, target, handler, context, allowRecursiveBinding);}private AggregateBinder<?> getAggregateBinder(Bindable<?> target, Context context) {Class<?> resolvedType = target.getType().resolve(Object.class);if (Map.class.isAssignableFrom(resolvedType)) {return new MapBinder(context);}if (Collection.class.isAssignableFrom(resolvedType)) {return new CollectionBinder(context);}if (target.getType().isArray()) {return new ArrayBinder(context);}return null;}private <T> Object bindAggregate(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler,Context context, AggregateBinder<?> aggregateBinder) {AggregateElementBinder elementBinder = (itemName, itemTarget, source) -> {boolean allowRecursiveBinding = aggregateBinder.isAllowRecursiveBinding(source);Supplier<?> supplier = () -> bind(itemName, itemTarget, handler, context, allowRecursiveBinding, false);return context.withSource(source, supplier);};return context.withIncreasedDepth(() -> aggregateBinder.bind(name, target, elementBinder));}private <T> Object bindProperty(Bindable<T> target, Context context, ConfigurationProperty property) {context.setConfigurationProperty(property);Object result = property.getValue();result = this.placeholdersResolver.resolvePlaceholders(result);result = context.getConverter().convert(result, target);return result;}
bindObject方法先通过findProperty获取ConfigurationProperty,然后执行bindAggregate或者bindProperty;AggregateBinder主要是处理Map、Collection、Array类型;bindProperty方法这里从property获取绑定的值,然后resolvePlaceholders,最后通过converter的convert方法把值绑定到target上
BindConverter
org/springframework/boot/context/properties/bind/BindConverter.java
<T> T convert(Object value, ResolvableType type, Annotation... annotations) {if (value == null) {return null;}return (T) this.conversionService.convert(value, TypeDescriptor.forObject(value),new ResolvableTypeDescriptor(type, annotations));}
BindConverter的convert方法则是通过conversionService进行
小结
ConfigurationPropertiesBindingPostProcessor实现了BeanPostProcessor、PriorityOrdered、ApplicationContextAware、InitializingBean四个接口;其getOrder方法返回的是Ordered.HIGHEST_PRECEDENCE + 1
即仅次于最高的优先级;其postProcessBeforeInitialization方法主要是执行bind方法(先通过ConfigurationPropertiesBean.get获取ConfigurationPropertiesBean,再通过binder进行bind
);其afterPropertiesSet主要是获取BeanDefinitionRegistry与ConfigurationPropertiesBinder
相关文章:

聊聊springboot的ConfigurationProperties的绑定
序 本文主要研究一下springboot的ConfigurationProperties的绑定 ConfigurationPropertiesBindingPostProcessor org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java /*** {link BeanPostProcessor} to bind {link PropertySo…...

Mysql和Oracle的语法区别?
Mysql和Oracle是两种不同的关系型数据库。 MySQL通常在中小型应用程序、Web应用程序和小型企业中广泛使用,因为它易于学习和部署,而且成本较低。 Oracle数据库通常用于大型企业和复杂的企业级应用程序,因为它提供了高度可扩展性、高可用性…...

F - LIS on Tree
F - LIS on Tree (atcoder.jp) 问题描述:树上LIS。 普通LIS。O(n * n)。 void solve() {int n; cin>>n;vector<int> f(n 1),a(n1);for(int i 1; i < n; i) {cin>>a[i];f[i] 1;for(int j 1; j < i; j) {if(a[i] > a[j]) f[i] max…...

2023 年全国大学生数学建模B题目-多波束测线问题
B题目感觉属于平面几何和立体几何的问题,本质上需要推导几何变换情况,B题目属于有标准答案型,没太大的把握不建议选择,可发挥型不大。 第一问 比较简单,就一个2维平面的问题,但有点没理解,这个…...

qt creater11 翻译国际化教程教程:
先出效果图。 闲聊几句:qt这个翻译很方便,能直接导出项目里所有文字。 具体步骤如下: 在Qt中,我们可以使用QTranslator类来实现多语言切换。以下是一般步骤: 1. 在你的源代码中,所有需要翻译的字符串都…...

【AWS实验 】在 AWS Fargate 上使用 Amazon ECS 部署应用程序
文章目录 实验概览目标实验环境任务 1:连接到实验命令主机任务 2:将应用程序容器化任务 3:构建 Web2048 容器任务 4:创建 Amazon ECR 存储库并推送 Docker 映像任务 5:创建 ECS 集群任务 6:测试应用程序总结…...

matlab几种求解器的选择fsolve-sole-vpasolve
文章目录 fsolvesolvevpasovle总结vpasovle的结果fsovle的结果 fsolve 求数值解 result_xfsolve(my_fun,x0,options)参数: my_fun:待求解函数,作为一个.m文件 x0:初始值,向量,可以仅仅指定其中的几项solve 强大的求解器。在方程组中求解析…...

无限访问 GPT-4,OpenAI 强势推出 ChatGPT 企业版!
继 ChatGPT 收费大降价、推出 App 版等系列动作之后,OpenAI 于今日宣布正式发布面向企业的 AI 助手——ChatGPT Enterprise 版。 与 To C 端的 ChatGPT 版本有所不同的是,该版本可以以更快速度无限制地访问 GPT-4,还可以用来处理更长输入的上…...

MySQL的故事——Schema与数据类型优化
Schema与数据类型优化 一、选择优化的数据类型 更小的通常更好 应该尽量使用可以正确存储数据的最小类型,更小的数据类型通常更快,因为他们占用更少的磁盘,内存和CPU缓存,并且处理时需要的CPU周期更少 简单就好 更简单的数据类型…...

C++编译和链接
编译和链接 一、源代码的组织 头文件(.h):#include头文件、函数的声明、结构体的声明、类的声明、模板的声明、内联函数、#define和const定义的常量等。 源文件(.cpp):函数的定义、类的定义、模板具体化的…...
【CSDN技术】Markdown编辑器如何使用-csdn博客编写入门
Markdown编辑器如何使用-csdn博客编写入门 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自…...

【docker】运行redis
拉取redis镜像 有多种选择: redis(基础版)redis/redis-stack(包含redis stack server和RedisInsight)redis/redis-stack-server(仅包含redis stack server) docker pull redis docker pull r…...

Paddle训练COCO-stuff数据集学习记录
COCO-stuff数据集 COCO-Stuff数据集对COCO数据集中全部164K图片做了像素级的标注。 80 thing classes, 91 stuff classes and 1 class ‘unlabeled’ 数据集下载 wget --directory-prefixdownloads http://images.cocodataset.org/zips/train2017.zip wget --directory-prefi…...

SpringBoot 框架学习
java 学习笔记指路 基础知识 Python转java补充知识 Java中常见的名词解释 前端 【黑马程序员pink老师前端】HTML 【黑马程序员pink老师前端】JavaScript基础大总结 【黑马程序员pink老师前端】JavaScript函数与作用域 【黑马程序员pink老师前端】JavaScript对象 数据库 【黑马程…...

java - lua - redis 完成商品库存的删减
java调用lua脚本完成对商品库存的管理 主页链接 微风轻吟挽歌的主页 如若有帮助请帮忙点赞 //lua脚本 获取到内存不够的商品StringBuilder sb new StringBuilder();//定义一个数组存储可能缺少库存的值sb.append(" local table {} ");//获取值sb.append(" …...

dbeaver离线安装clickhouse连接驱动
Clickhouse 数据库连接工具——DBeaver 主要介绍了Clickhouse 数据库连接工具——DBeaver相关的知识,希望对你有一定的参考价值。 Clickhouse 数据库连接工具——DBeaver 1.下载 DBeaver 和 连接驱动 https://dbeaver.io/files/dbeaver-ce-latest-x86_64-setup.…...

2024腾讯校招后端面试真题汇总及其解答(二)
11.如果同时有5个任务在10分钟之后提交,或者更多,那么如果是一个个从队列中拿数据,那么前一个任务会影响后续任务执行时间,说一下解决思路 你的问题是一个典型的并发处理问题。如果你的系统是单线程的,那么的确,前一个任务的执行时间会影响后续任务的执行时间。但是,你…...

datagrip 相关数据连接信息无缝迁移
背景 因为公司换电脑了,接触的项目比较多,不同项目,不同环境的数据库连接有好几十个,如果在新电脑上挨个重新连接一遍劳心劳力,所以想看一下能不能直接将之前保存的连接信息直接迁移到新的电脑上面。 为此,…...

不就是G2O嘛
从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码 SLAM的后端一般分为两种处理方法,一种是以扩展卡尔曼滤波(EKF)为代表的滤波方法,一种是以图优化为代表的非线性优化方法。不过,目前SLAM研究的主…...

C#开发的OpenRA游戏之系统参数选项按钮
C#开发的OpenRA游戏之系统参数选项按钮 前面分析了信标按钮,从图上可以看到,靠右边的按钮,就是系统参数选项按钮: 这个按钮与前面三个按钮是不一样的,虽然它们在排列位置上是放在一起,但是处理的方法方式是不一样的,因为这个选项按钮,并不需要发命令给服务器,再返回来…...

苹果启动2024年SRDP计划:邀请安全专家使用定制iPhone寻找漏洞
苹果公司昨天(8月30日)正式宣布开始接受2024 年iPhone安全研究设备计划的申请,iOS 安全研究人员可以在 10 月底之前申请安全研究设备 SRD。 SRD设备是专门向安全研究人员提供的iPhone14Pro,该设备具有专为安全研究而设计的特殊硬…...

std::make_shared和new初始化智能指针的区别
先看代码: class Base {public:Base(int num):a(num) {std::cout << "Base() construct" << std::endl;}~Base() {std::cout << "Base() deconstruct" << std::endl;}int Get() {return a;}private:int a; };void tes…...

无涯教程-JavaScript - ERFC.PRECISE函数
描述 ERFC.PRECISE函数返回x和无穷大之间集成的互补ERF函数。 互补误差函数等于1-ERF(即1-误差函数),由等式给出- $$Erfc(x) \frac {2} {\sqrt {\pi}} \int_ {x} ^ {\infty} e ^ {-t ^ 2} dt $$ 语法 ERFC.PRECISE(x)争论 Argument描述Required/OptionalxThe lower bound…...

2023国赛数学建模C题思路分析 - 蔬菜类商品的自动定价与补货决策
# 1 赛题 在生鲜商超中,一般蔬菜类商品的保鲜期都比较短,且品相随销售时间的增加而变差, 大部分品种如当日未售出,隔日就无法再售。因此, 商超通常会根据各商品的历史销售和需 求情况每天进行补货。 由于商超销售的蔬菜…...

手写Spring:第1章-开篇介绍,手写Spring
文章目录 一、手写Spring二、Spring 生命周期 一、手写Spring 💡 目标:我们该对 Spring 学到什么程度?又该怎么学习呢? 手写简化版 Spring 框架,了解 Spring 核心原理,为后续再深入学习 Spring 打下基础。在…...

C语言中,字节对齐是一种重要的内存管理概念
C语言中,字节对齐是一种重要的内存管理概念 字节对齐的目的是为了提高内存访问的效率。因为CPU访问内存的最小单位是字节,所以如果数据结构的成员以正确的字节边界对齐,那么CPU就可以直接访问这些成员,而不需要进行额外的内存移动…...

网络丢包问题,敢不敢这样定位?
下午好,我的网工朋友。 所谓丢包,是指在网络数据的收发过程中,由于种种原因,数据包还没传输到应用程序中,就被丢弃了。 这些被丢弃包的数量,除以总的传输包数,也就是我们常说的丢包率。 丢包…...

【漏洞复现】H3C路由器信息泄露任意用户登录
漏洞描述 通过访问特地址得到密码可进行登录。 免责声明 技术文章仅供参考,任何个人和组织使用网络应当遵守宪法法律,遵守公共秩序,尊重社会公德,不得利用网络从事危害国家安全、荣誉和利益,未经授权请勿利用文章中…...

随机数算法,SQL
SELECT* FROMprizes_config WHEREweight > ( SELECT FLOOR( RAND() * MAX( weight )) FROM prizes_config ) order by weight asc-- LIMIT 1;记录 id 权重 1 5 2 10 3 50 4 100 找权重最大的那个值,调用rand()函数&#…...

什么是软件测试+软件测试的分类【软件测试】
软件测试 什么是软件? 软件 程序 数据 文档 软件测试的对象有哪些?程序 数据 文档 C/S与B/S架构 C/S:客户端服务器,这种就是我们一定要安装一个客户端才能够用的软件,就叫C/S。比如:微信、qq B/S&am…...