温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-02-17 13:35:43 来源:亿速云 阅读:232 作者:iii 栏目:开发技术

这篇文章主要介绍“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;

测试

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

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

application.yml

Java怎么使用ConfigurationProperties获取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中的配置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI