千家信息网

Java怎么使用ConfigurationProperties获取yml中的配置

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要介绍"Java怎么使用ConfigurationProperties获取yml中的配置"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Java怎么使
千家信息网最后更新 2025年11月07日Java怎么使用ConfigurationProperties获取yml中的配置

这篇文章主要介绍"Java怎么使用ConfigurationProperties获取yml中的配置"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Java怎么使用ConfigurationProperties获取yml中的配置"文章能帮助大家解决问题。

使用ConfigurationProperties获取yml的配置

我们在开发过程中,会经常遇到需要自定义配置的场景,比如配置一个ip,一个地址等,并将其写入到yml文件中,在项目中使用@Value("${xxxx.xxxx}")来获取自定义的配置,其实是这样是有些笨重的,每定义一个配置,都需要写一个@Value来获取,那为啥不使用一个java config来统一获取配置呢?

使用方法

编写yml配置文件

user:  config:    # user_name user-name userName这三种配置方式都可以被识别到    user_name: "zhangsan"    age: "20"    exmail: "123@123.com"    address: "火星"

编写Java config类

// 需要重写get与set方法,此处使用lombok注解来代替@Data// 实例化到spring容器中@Component// 获取前缀为user.config的配置信息,与该类下的属性对比,进行绑定@ConfigurationProperties(prefix = "user.config")public class UserConfig {    private String userName;    private String age;    private String exmail;    private String address;}

在需要使用的地方注入

    @Resource    private UserConfig userConfig;

测试

@ConfigurationProperties获取不到配置文件属性值问题

application.yml

配置类

@Component@ConfigurationProperties(prefix = "system")public class SystemConfig {    /**     * 项目名称     */    private static String name;    /**     * 版本     */    private String version;    /**     * 版权年份     */    private String copyrightYear;    /**     * 实例演示开关     */    private boolean demoEnabled;    /**     * windows环境下,文件上传路径(本地上传)     */    private static String winUploadPath;    /**     * 其他系统环境(linux、Mac...)环境下,文件上传路径(本地上传)     */    private static String otherUploadPath;    /**     * 获取地址开关     */    private static boolean addressEnabled;    public static String getName() {        return name;    }    public void setName(String name) {        SystemConfig.name = name;    }    public String getVersion() {        return version;    }    public void setVersion(String version) {        this.version = version;    }    public String getCopyrightYear() {        return copyrightYear;    }    public void setCopyrightYear(String copyrightYear) {        this.copyrightYear = copyrightYear;    }    public boolean isDemoEnabled() {        return demoEnabled;    }    public void setDemoEnabled(boolean demoEnabled) {        this.demoEnabled = demoEnabled;    }    public static String getWinUploadPath() {        return winUploadPath;    }    public static void setWinUploadPath(String winUploadPath) {        SystemConfig.winUploadPath = winUploadPath;    }    public static String getOtherUploadPath() {        return otherUploadPath;    }    public static void setOtherUploadPath(String otherUploadPath) {        SystemConfig.otherUploadPath = otherUploadPath;    }    public static boolean isAddressEnabled() {        return addressEnabled;    }    public void setAddressEnabled(boolean addressEnabled) {        SystemConfig.addressEnabled = addressEnabled;    }    /**     * 判断当前操作系统,返回相应的本地上传路径     *     * @return String     * @author Liangyixiang     * @date 2021/11/15     **/    public static String getUploadPath() {        OsInfo osInfo = SystemUtil.getOsInfo();        // 判断系统环境获取上传路径        if(ObjectUtils.isNotEmpty(osInfo) && osInfo.isWindows()){            return getWinUploadPath();        }else{            return getOtherUploadPath();        }    }    /**     * 获取业务系统名称     */    public static String getSystemName() {        return getName();    }}

name、addressEnabled 以及 winUploadPath、otherUploadPath 都是静态的成员变量,但是他们name、addressEnabled能获取到配置文件的值,winUploadPath、otherUploadPath不可以。

原因就是

winUploadPath、otherUploadPath对应的ser方法也定义为了静态方法。

关于"Java怎么使用ConfigurationProperties获取yml中的配置"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

0