Java如何使用ConfigurationProperties获取yml中的配置

2022-07-18,,,,

目录
  • 使用configurationproperties获取yml配置
    • 使用方法
  • @configurationproperties获取不到配置文件属性值问题
    • 原因就是

使用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中的配置.doc》

下载本文的Word格式文档,以方便收藏与打印。