类上注解@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;
}
}
}
被读取文件位置

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

1万+

被折叠的 条评论
为什么被折叠?



