Spring Boot 引入自定义yml

时间:2023-03-08 19:41:04
Spring Boot 引入自定义yml

喜欢yml配置文件格式的人性化,也喜欢properties配置文件管理方式的人性化,

那么下面我们就来看一下 yml 是如何配置和使用类似properties管理方式的人性化。

配置文件

设置Spring Boot 系统 yml 和自定义 yml文件

application.yml


spring:
profiles:
active: dev
include: test #或者 include: "test"
application:
name: test-yml-application

application-test.yml


test:
msg: 这不就是配置文件的内容吗

基于抽象类的使用

常见的有两种方式

方式一

使用 @Value

AbstractCp


public abstract class AbstractCp { @Value("${test.msg}")
private String msg; public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

Cp


@Component
public class Cp extends AbstractCp { }

方式二

使用 @ConfigurationProperties

AbstractCp


@EnableConfigurationProperties
@ConfigurationProperties("test")
public abstract class AbstractCp { private String msg; public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

Cp


@Component
public class Cp extends AbstractCp { }

作者:随风浮云

出处:http://www.cnblogs.com/ljmatlight

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,

且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。