SystemMessage.java读取配置文件到内存工具类

SystemMessage.java是一个Spring组件,用于从user-details.properties资源文件中加载和处理用户详细信息。它使用Properties类读取文件中的键值对,将用户名与角色关联,并提供获取不同数据类型(如字符串、整数、布尔值)的方法。如果文件未找到,日志会记录错误。

类上注解@Component可以去掉。

SystemMessage.java:

package com.genertech.plm.aia.login.util;

import com.genertech.plm.base.entity.ErrorCode;
import com.genertech.sia.local.log.api.util.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Slf4j
public class SystemMessage {
  private static Properties prop;
  public static Map<String, String> map = new HashMap();
  private static SystemMessage systemMessage = new SystemMessage();

  static {
    prop = new Properties();
    InputStream inputStream = null;
    try {
      inputStream = systemMessage.getClass().getResourceAsStream("/user-details.properties");
      prop.load(inputStream);
      Enumeration enumeration = prop.propertyNames();
      while (enumeration != null && enumeration.hasMoreElements()) {
        String username = (String) enumeration.nextElement();
        String monitorRole = SystemMessage.getString(username);
        log.info("user-details.properties User [{}] is in role [{}]", username, monitorRole);
        map.put(username, monitorRole);
      }
    } catch (Exception e) {
      log.error(LogUtil.formatErrorMessage(ErrorCode.B0, "file [" + "user-details.properties" + "] not found!"), e);
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

  }

  private SystemMessage() {
  }

  /**
   * get the value from the properties file.
   *
   * @param key the key in the properties file
   */
  public static String getString(String key) {
    try {
      return prop.getProperty(key);
    } catch (MissingResourceException e) {
      return '!' + key + '!';
    }
  }

  /**
   * 获取int类型.
   *
   * @param key NOTNULL
   * @return
   */
  public static Integer getInteger(String key) {
    try {
      String value = prop.getProperty(key);
      return Integer.valueOf(value);
    } catch (MissingResourceException e) {
      return 0;
    }
  }

  /**
   * 获取int类型配置,如没有返回defaultValue.
   *
   * @param key          NOTNULL
   * @param defaultValue NULL
   * @return
   */
  public static Integer getInteger(String key, int defaultValue) {
    try {
      String value = prop.getProperty(key);
      return Integer.valueOf(value);
    } catch (MissingResourceException e) {
      return defaultValue;
    }
  }

  /**
   * 获取boolean类型属性.
   *
   * @param key NOTNULL
   * @return
   */
  public static Boolean getBoolean(String key) {
    try {
      String value = prop.getProperty(key);
      return Boolean.valueOf(value);
    } catch (MissingResourceException e) {
      return false;
    }
  }
}

被读取文件位置

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值