(1)引入依赖支持YML:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-config-yaml</artifactId>
</dependency>
定义环境变量,使用YML方式:
student:
name: 高杨
age: 28
birthday: 1992-01-23
teacher: 老师好
普通方式:
@ConfigProperty(name = "teacher")
private String teacher;
使用对象方式:
@ConfigProperties(prefix = "student")
public class XStudent {
private String name;
private Integer age;
private LocalDate birthday;
@Inject
private XStudent xStudent;
(2)不同环境变量的指定:
使用%{profile}.config.key=value格式指定参数,当然YML相关转换为冒号
"%dev":
student:
name: 高杨-dev
age: 28
teacher: 老师好-dev
"%prod":
student:
name: 高杨-prod
age: 28
teacher: 老师好-prod
Quarkus内置了三种模式:
-
dev-在开发模式下(即
quarkus:dev
)激活 -
test-运行测试时激活
-
prod-未在开发或测试模式下运行时的默认配置文件
例如测试运行使用prod的变量:
mvn compile quarkus:dev -Dquarkus.profile=prod
打包:
./mvnw package -Pnative -Dquarkus.profile=prod
加入其他配置文件:
./mvnw quarkus:add-extension -Dextensions="config-yaml"
https://quarkus.io/guides/config
https://quarkus.io/guides/all-config