Spring Boot 获取 application.yml 配置文件属性值 Plus 版
多年前我曾写过这样一篇文章 SpringBoot通过@Value获取application.yml配置文件的属性值
没想到已经有 3 w 阅读了,这里做个升级,给大家提供一种更高级的配置方法
class 类如下
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = DyingGqConfig.PREFIX)
public class DyingGqConfig {
protected static final String PREFIX = "dying.stranded";
@Value("dying搁浅")
private String thisIsMy;
}
application.yml 文件配置如下
dying:
stranded:
this_is_my: dying搁浅plus
规则如下:
@ConfigurationProperties(prefix = DyingGqConfig.PREFIX)prefix 配置你的通用前缀@Value("dying搁浅")这里配置你的默认值,在没有配置 yml 文件值时默认填此值thisIsMy对应配置文件为this_is_my虽然 Spring 支持宽匹配,但我建议统一下划线就好。
最后
以上就是魔幻小懒虫最近收集整理的关于Spring Boot 获取 application.yml 配置文件属性值 Plus 版的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复