Springboot 项目读取yaml的配置文件信息给静态方法使用,以及通过配置 ResourceBundle 类读取config.properties

读取yaml 的配置文件

配置文件信息

iot_saas_tenement:
  user_id: 7........8d9b
  private_key: MII.......qQ==
  bj_url: http://4.....5:8088
  project_name: iot_s.......roject
  device_name: te.....ice

创建一个类 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配置文件信息

userId=7dd.......9b
private_key=MIIC........Q==
url=http://4......5:8088
project_name=iot_sa..............ect

定义读取 配置类 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  的信息

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值