网站首页 > 博客文章 正文
概述
Spring Boot的配置文件的格式有:`properties` 和 `yml(yaml)[官方推荐]`。当`properteis` 和 `yml(yaml)`同时存在时,按照如下规则加载:`yml>yaml>properties`。
从`yml>yaml>properties`可以看出,后加载的配置会将先加载的配置覆盖掉。
自定义配置
在开发中,通常我们需要自定义一些配置来实现相应的功能,那么,在Spring Boot中,我们该如何来实现呢?这里有三种方式供大家选择,
- @Value 注解
- @ConfigurationProperties 注解
- @ConfigurationProperties 和 @PropertySource 注解
@Value注解
@Component
public class ValueConfig {
@Value("${summer.config.title}")
public String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
@ConfigurationProperties注解
@ConfigurationProperties(prefix = "summer.config")
@Component
public class ConfigurationConfig {
public String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
@ConfigurationProperties 和 @PropertySource 注解
@ConfigurationProperties(prefix = "summer.binding.config")
@PropertySource("classpath:binding.properties")
@Component
public class ConfigurationBindFile {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
使用@ConfigurationProperties和 @PropertySource注解时,配置文件的格式,只能是properties的
多环境配置
我们以 yml 格式的配置文件为例,properties 和 yml 的多环境配置规则类似。通常我们有两种方式来实现多环境配置
- spring.profiles
- Maven Filter
spring.profiles方式
在该方式下,有如下两种实现形式
- 单文件,多profiles
- 多文件形式
单文件
spring:
profiles:
active: prod
---
spring:
profiles: dev
summer:
config:
title: spring boot learning dev
---
spring:
profiles: prod
summer:
config:
title: spring boot learning prod
多文件形式
summer:
config:
title: spring boot learning prod
配置内容,基本类似,只是存在两个配置文件
Maven Filter 方式
在该配置方式下,一套环境就是一个properties 文件,application中不含具体的配置,而都是占位符,Maven 打包时候通过具体的profile 将application 中的占位符替换掉。
1. 首先我们需要修改之前的application.yml中的配置内容
summer:
config:
title: @summer.config.title@
2. 在src/main目录下,新建filters目录,并添加如下两个文件,注意,要与java文件同级
内容如下:
# dev.proerties
summer.config.title=spring boot learning dev
# prod.proerties
summer.config.title=spring boot learning prod
3. 在`pom.xml`中添加如下内容
<build>
<filters>
<filter>${props}</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>binding.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<fork>true</fork>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<props>src/main/filters/dev.properties</props>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<props>src/main/filters/prod.properties</props>
</properties>
</profile>
</profiles>
该配置方式下,打包时只会打包指定环境的配置文件
命令如下:
mvn clean package -DMaven.test.skip=true -Pprod
mvn clean package -DMaven.test.skip=true -Pdev
外部化配置
springboot默认支持外部化配置,以该工程为例,你可以直接将application.yml中的配置,外迁到jar包外的同级目录,springboot会自动扫描并加载配置文件。
好了,今天的记录差不多接到此结束。接下来我们一起来看下在配置以下特殊配置的时候,该怎么来操作。下一章我们再见!!!
伙伴们,行动起来,加油!!!
猜你喜欢
- 2025-01-15 kubernetes快速安装部署指南
- 2025-01-15 一文带你看懂vue基础语法、生命周期、指令、监听器、依赖注入
- 2025-01-15 美国工程合同中常见的招投标条款(中英文对照)
- 2025-01-15 JNI接口操作最佳实践(三)
- 2025-01-15 java基础都在这了,小主们拿去吧
- 2025-01-15 写作技巧——这些情况下,必须加连字符
- 2025-01-15 「云原生」k8s中volumeMounts.subPath的巧妙用法
- 2025-01-15 RmlUi:探索C++图形用户界面的璀璨明珠
- 2025-01-15 超级好用的配置文件解析器:minIni
- 2025-01-15 windows常见后门和Linux常见后门
你 发表评论:
欢迎- 366℃用AI Agent治理微服务的复杂性问题|QCon
- 358℃初次使用IntelliJ IDEA新建Maven项目
- 353℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 351℃Maven技术方案最全手册(mavena)
- 348℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 346℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 344℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 342℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)