配置文件详解 📄

Spring Boot 默认支持 application.propertiesapplication.yml 配置文件,可通过以下方式扩展:

  • 多配置文件:使用 application-{profile}.properties 实现多环境配置(如 dev/prod
  • 嵌套配置:通过 . 分隔符支持层级结构,例如 database.url=jdbc:mysql://localhost:3306/mydb
  • 类型安全配置:结合 @ConfigurationProperties 实现自定义配置类

配置属性优先级 📌

Spring Boot 会按以下顺序加载配置(优先级从高到低):

  1. 命令行参数(--key=value
  2. 系统环境变量(KEY=value
  3. 操作系统环境变量
  4. application-{profile}.properties(当前profile)
  5. application.properties(默认)
  6. 默认属性(SpringApplication.setDefaultProperties()

企业级配置技巧 💼

  • 配置加密:集成 Spring Cloud Config 实现安全配置管理
  • 动态配置:使用 @RefreshScope 实现配置热更新
  • 配置校验:通过 @Validated 注解对配置参数进行校验
  • 配置监控:结合 Actuator/actuator/configprops 接口查看配置信息

扩展阅读 📚

想要了解更多基础配置内容,可访问 Spring Boot 配置入门文档 进行学习。

Spring_Boot_Config