0
点赞
收藏
分享

微信扫一扫

腾讯:《智能科技 跨界相变——2024数字科技前沿应用趋势》

哈哈镜6567 03-03 21:30 阅读 2

读取yaml 的配置文件

配置文件信息

创建一个类 ProxyProperties 读取配置文件信息,并对外提供get方法

package com.purvardata.himp.third.bj.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

// 获取yaml的配置信息添加到静态方法
@Component
public final  class ProxyProperties {


    @Value("${iot_saas_tenement.bj_url}")
    private String bj_url;
    private static String url;

    @Value("${iot_saas_tenement.user_id}")
    private String user_id;
    private static String userId;

    @Value("${iot_saas_tenement.private_key}")
    private String private_key;
    private static String privateKey;

    @Value("${iot_saas_tenement.project_name}")
    private String project_name;
    private static String projectName;

    @Value("${iot_saas_tenement.device_name}")
    private String device_name;
    private static String deviceName;



    @PostConstruct
    public void setUrl() {
        url=this.bj_url;
        userId=this.user_id;
        privateKey=this.private_key;
        projectName=this.project_name;
        deviceName=this.device_name;
    }

    public static String getUrl() {
        return url;
    }

    public static String getUserId() {
        return userId;
    }

    public static String getPrivateKey() {
        return privateKey;
    }

    public static String getProjectName() {
        return projectName;
    }

    public static String getDeviceName() {
        return deviceName;
    }
}

目标静态方法通过get方法获取对应的属性

通过类 ResourceBundle 读取 config.properties 的配置文件

config.properties配置文件信息

定义读取 配置类 PropertiesUtils,注意 config.properties 目录,要是和 ResourceBundle.getBundle("config")路径一致,我这里放根路径了

package com.iline.bj;

import java.util.ResourceBundle;

public class PropertiesUtils {


    private static ResourceBundle bundle = ResourceBundle.getBundle("config");

    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public static String getValue(String key) {
        return bundle.getString(key);
    }

}

使用配置类 PropertiesUtils.getValue 获取配置文件 config.properties  的信息

举报

相关推荐

0 条评论