package com.sjz.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 配置工具类 * * @author sjz */ public class ConfigUtil { private static final Properties properties = new Properties(); static { try (InputStream is = ConfigUtil.class.getClassLoader().getResourceAsStream("application.properties")) { if (is != null) { properties.load(is); } } catch (IOException e) { throw new RuntimeException("Failed to load application.properties", e); } } /** * 获取配置值 */ public static String getProperty(String key) { return properties.getProperty(key); } /** * 获取配置值,如果不存在则返回默认值 */ public static String getProperty(String key, String defaultValue) { return properties.getProperty(key, defaultValue); } /** * 获取整数配置值 */ public static int getIntProperty(String key, int defaultValue) { String value = getProperty(key); if (value == null) { return defaultValue; } try { return Integer.parseInt(value); } catch (NumberFormatException e) { return defaultValue; } } /** * 获取布尔配置值 */ public static boolean getBooleanProperty(String key, boolean defaultValue) { String value = getProperty(key); if (value == null) { return defaultValue; } return Boolean.parseBoolean(value); } /** * 获取长整数配置值 */ public static long getLongProperty(String key, long defaultValue) { String value = getProperty(key); if (value == null) { return defaultValue; } try { return Long.parseLong(value); } catch (NumberFormatException e) { return defaultValue; } } /** * 检查必要的配置是否存在 */ public static void validateRequiredProperties(String... requiredKeys) { for (String key : requiredKeys) { String value = getProperty(key); if (value == null || value.trim().isEmpty()) { throw new RuntimeException("Required property '" + key + "' is missing or empty"); } } } /** * 获取Tushare Token */ public static String getTushareToken() { String token = getProperty("tushare.token"); if (token == null || token.equals("your_tushare_token_here")) { throw new RuntimeException("Tushare token is not configured properly"); } return token; } /** * 获取数据库URL */ public static String getDatabaseUrl() { return getProperty("db.url"); } /** * 获取数据库用户名 */ public static String getDatabaseUsername() { return getProperty("db.username"); } /** * 获取数据库密码 */ public static String getDatabasePassword() { String password = getProperty("db.password"); if (password == null || password.equals("your_password_here")) { throw new RuntimeException("Database password is not configured properly"); } return password; } }