• 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方法

    1. package com.purvardata.himp.third.bj.utils;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.stereotype.Component;
    4. import javax.annotation.PostConstruct;
    5. // 获取yaml的配置信息添加到静态方法
    6. @Component
    7. public final class ProxyProperties {
    8. @Value("${iot_saas_tenement.bj_url}")
    9. private String bj_url;
    10. private static String url;
    11. @Value("${iot_saas_tenement.user_id}")
    12. private String user_id;
    13. private static String userId;
    14. @Value("${iot_saas_tenement.private_key}")
    15. private String private_key;
    16. private static String privateKey;
    17. @Value("${iot_saas_tenement.project_name}")
    18. private String project_name;
    19. private static String projectName;
    20. @Value("${iot_saas_tenement.device_name}")
    21. private String device_name;
    22. private static String deviceName;
    23. @PostConstruct
    24. public void setUrl() {
    25. url=this.bj_url;
    26. userId=this.user_id;
    27. privateKey=this.private_key;
    28. projectName=this.project_name;
    29. deviceName=this.device_name;
    30. }
    31. public static String getUrl() {
    32. return url;
    33. }
    34. public static String getUserId() {
    35. return userId;
    36. }
    37. public static String getPrivateKey() {
    38. return privateKey;
    39. }
    40. public static String getProjectName() {
    41. return projectName;
    42. }
    43. public static String getDeviceName() {
    44. return deviceName;
    45. }
    46. }

    目标静态方法通过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")路径一致,我这里放根路径了

    1. package com.iline.bj;
    2. import java.util.ResourceBundle;
    3. public class PropertiesUtils {
    4. private static ResourceBundle bundle = ResourceBundle.getBundle("config");
    5. /**
    6. * 获取值
    7. *
    8. * @param key
    9. * @return
    10. */
    11. public static String getValue(String key) {
    12. return bundle.getString(key);
    13. }
    14. }

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

  • 相关阅读:
    计算机毕业设计Java城市交通海量数据管理系统(源码+系统+mysql数据库+lw文档)
    在群晖NAS部署_开源在线项目任务管理工具【dooTask】
    使用华为eNSP组网试验⑶-OSPF单区域组网
    noexcept 修饰符
    Github的2FA验证问题的丝滑解决方案 ||(Verify your two-factor authentication (2FA) settings)
    如何平衡新老策略的好与坏,一道常见风控送命题解答
    这几个Matplotlib绘图技巧,真的是太实用了
    JVM与Java体系结构
    2022年重庆交通大学自考毕业证书发放时间
    【博客500】k8s调度器如何为pod计算最终得分
  • 原文地址:https://blog.csdn.net/qq_36961226/article/details/136395161