当前位置: 首页 > news >正文

SpringBoot 属性配置解析

属性配置介绍

spring官方提供的17中属性配置的方式

  1. Devtools全局配置
  2. 测试环境@TestPropertySource注解
  3. 测试环境properties属性
  4. 命令行参数
  5. SPRING_APPLICATION_JSON属性
  6. ServletConfig初始化参数
  7. ServletContext初始化参数
  8. JNDI属性
  9. JAVA系统属性
  10. 操作系统环境变量
  11. RandomValuePropertySource随机值属性
  12. jar包外的application-{profile}.properties
  13. jar包内的application-{profile}.properties
  14. jar包外的application.properties
  15. jar包内的application.properties
  16. @PropertySource绑定配置
  17. 默认属性

Spring Aware介绍

Aware介绍

  • Spring框架优点:Bean感知不到容器的存在
  • 使用场景:需要使用Spring容器的功能资源
  • 引入缺点:Bean和容器强耦合

常用Aware

Aware调用

自定义实现Aware

import org.springframework.beans.factory.Aware;public interface MyAware extends Aware {void setFlag(Flag flag);}
@Component
public class MyAwareProcessor implements BeanPostProcessor {private final ConfigurableApplicationContext configurableApplicationContext;public MyAwareProcessor(ConfigurableApplicationContext configurableApplicationContext) {this.configurableApplicationContext = configurableApplicationContext;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof Aware) {if (bean instanceof MyAware) {((MyAware) bean).setFlag((Flag) configurableApplicationContext.getBean("flag"));}}return bean;}
}

Environment解析

getOrCreateEnvironment

  • 添加servletConfigInitParams属性集
  • 添加servletContextInitParams属性集
  • 添力Jndi属性集
  • 添加systemProperties属性集
  • 添加systemEnvironment属性集

configureEnvironment

  • 添加defaultProperties厲性集
  • 添加commandLineArgs属性集

listener.environmentPrepared

  • 添加spring_application_json属性集
  • 添加vcap属性集
  • 添加random属性集
  • 添加application-profile.(properties | yml)属性集

ConfigurationPropertySources.attach

  • 添加configurationProperties属性集

ConfigurationClassParser

  • 添加@PropertySources属性集

Spring profile介绍

将不同的参数配置绑定在不同的环境

默认使用

  • application.properties
  • application-default.properties

激活profile

  • spring.profiles.active=xx
  • spring.profiles.activedefault互斥(即配置active后application-default.properties会失效)

Spring profile原理解析

源码流程解析

处理入口

initializeProfiles逻辑

profiles处理

profile处理逻辑

Load逻辑

addLoadedPropertySources

面试题

  • SpringBoot属性配置方式有哪些?
  • 介绍下Spring Aware的作用及常见的有哪些?
  • Spring Aware注入原理?
  • 动手写一个Spring Aware?
  • Environment对象是如何加载属性集的?
  • 部分属性集如spring_application_json何时被加载的?
  • 介绍下Spring Profile?常用配置方式?
  • Spring Profile配置方式有哪些注意事项,为什么?
  • Spring Profile处理逻辑?

相关文章:

  • VSCode配置记录
  • 手把手教你写 Compose 动画 -- 过渡动画 API:Transition
  • 微信小程序 - PC端选择ZIP文件
  • 《Vue.js设计与实现》—Vue3响应系统的原理
  • 腾讯地图系列(二):微信小程序添加插件(三种方法)以及插件AppId获取
  • ArcGIS无法绘制一个或多个图层
  • 基于以太坊的智能合约开发Solidity(数组提升篇)
  • 智能时代:互联网+如何改变我们的生活与工作
  • STM32CubeIDE串口空闲中断实现不定长数据接收
  • 华为OD机试真题-智能成绩表-2023年OD统一考试(C卷)
  • 线性代数入门与学习笔记
  • ViT:视觉 Transformer
  • VS Code 上已达250万 Java 开发者!微软和红帽一起公布 VS Code Java 未来六个月路线图
  • 机器学习实验三:支持向量机模型
  • 连锁零售企业如何优化网络性能?
  • [iOS]Core Data浅析一 -- 启用Core Data
  • [数据结构]链表的实现在PHP中
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • css布局,左右固定中间自适应实现
  • CSS中外联样式表代表的含义
  • Django 博客开发教程 16 - 统计文章阅读量
  • Hexo+码云+git快速搭建免费的静态Blog
  • Java反射-动态类加载和重新加载
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • Spring Cloud Feign的两种使用姿势
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 如何在GitHub上创建个人博客
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • ​2021半年盘点,不想你错过的重磅新书
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • $(selector).each()和$.each()的区别
  • ${ }的特别功能
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第1节 (全局数据、栈和堆)
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (差分)胡桃爱原石
  • (实战篇)如何缓存数据
  • (转) Face-Resources
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .NET Project Open Day(2011.11.13)
  • .netcore 获取appsettings
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .net打印*三角形
  • .NET企业级应用架构设计系列之应用服务器
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .NET业务框架的构建
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • ?
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • @hook扩展分析
  • [ C++ ] STL_list 使用及其模拟实现