当前位置: 首页 > 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 未来六个月路线图
  • 机器学习实验三:支持向量机模型
  • 连锁零售企业如何优化网络性能?
  • bearychat的java client
  • docker容器内的网络抓包
  • ECS应用管理最佳实践
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • Github访问慢解决办法
  • Java编程基础24——递归练习
  • js操作时间(持续更新)
  • JS数组方法汇总
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • webgl (原生)基础入门指南【一】
  • 成为一名优秀的Developer的书单
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 算法-插入排序
  • 在Mac OS X上安装 Ruby运行环境
  • elasticsearch-head插件安装
  • Java数据解析之JSON
  • puppet连载22:define用法
  • 湖北分布式智能数据采集方法有哪些?
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • #git 撤消对文件的更改
  • $.ajax()
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (二十三)Flask之高频面试点
  • (七)Knockout 创建自定义绑定
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • .bat批处理出现中文乱码的情况
  • .dwp和.webpart的区别
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET DevOps 接入指南 | 1. GitLab 安装
  • .NET与 java通用的3DES加密解密方法
  • /deep/和 >>>以及 ::v-deep 三者的区别
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [100天算法】-目标和(day 79)