|
@@ -12,7 +12,9 @@ import java.util.stream.Collectors;
|
|
import com.beust.jcommander.internal.Sets;
|
|
import com.beust.jcommander.internal.Sets;
|
|
import com.chelvc.framework.base.util.ObjectUtils;
|
|
import com.chelvc.framework.base.util.ObjectUtils;
|
|
import com.chelvc.framework.base.util.StringUtils;
|
|
import com.chelvc.framework.base.util.StringUtils;
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
import lombok.NonNull;
|
|
import lombok.NonNull;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
|
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
|
@@ -33,6 +35,7 @@ import org.springframework.stereotype.Component;
|
|
* @author Woody
|
|
* @author Woody
|
|
* @date 2023/4/5
|
|
* @date 2023/4/5
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
@Component
|
|
@Component
|
|
public class ApplicationContextHolder implements ApplicationContextAware {
|
|
public class ApplicationContextHolder implements ApplicationContextAware {
|
|
/**
|
|
/**
|
|
@@ -51,6 +54,11 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
*/
|
|
*/
|
|
private static ApplicationContext APPLICATION_CONTEXT;
|
|
private static ApplicationContext APPLICATION_CONTEXT;
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
|
+ APPLICATION_CONTEXT = applicationContext;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取应用上下文对象
|
|
* 获取应用上下文对象
|
|
*
|
|
*
|
|
@@ -60,11 +68,6 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
return Objects.requireNonNull(APPLICATION_CONTEXT, "Application context has not been initialized");
|
|
return Objects.requireNonNull(APPLICATION_CONTEXT, "Application context has not been initialized");
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
|
- APPLICATION_CONTEXT = applicationContext;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 获取应用环境变量信息
|
|
* 获取应用环境变量信息
|
|
*
|
|
*
|
|
@@ -84,6 +87,21 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
return getEnvironment().getProperty(key);
|
|
return getEnvironment().getProperty(key);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取安全的环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @return 变量值
|
|
|
|
+ */
|
|
|
|
+ public static String getSafeProperty(String key) {
|
|
|
|
+ try {
|
|
|
|
+ return getProperty(key);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("Application property get failed: {}, {}", key, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取环境变量值
|
|
* 获取环境变量值
|
|
*
|
|
*
|
|
@@ -95,6 +113,22 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
return getEnvironment().getProperty(key, defaultValue);
|
|
return getEnvironment().getProperty(key, defaultValue);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取安全的环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param defaultValue 默认值
|
|
|
|
+ * @return 变量值
|
|
|
|
+ */
|
|
|
|
+ public static String getSafeProperty(String key, String defaultValue) {
|
|
|
|
+ try {
|
|
|
|
+ return getProperty(key, defaultValue);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("Application property get failed: {}, {}", key, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取环境变量值
|
|
* 获取环境变量值
|
|
*
|
|
*
|
|
@@ -107,6 +141,52 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
return getEnvironment().getProperty(key, targetType);
|
|
return getEnvironment().getProperty(key, targetType);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取安全的环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 变量值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getSafeProperty(String key, Class<T> targetType) {
|
|
|
|
+ try {
|
|
|
|
+ return getProperty(key, targetType);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("Application property get failed: {}, {}", key, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 变量值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getProperty(String key, TypeReference<T> targetType) {
|
|
|
|
+ return JacksonContextHolder.deserialize(getProperty(key), targetType);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取安全的环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 变量值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getSafeProperty(String key, TypeReference<T> targetType) {
|
|
|
|
+ try {
|
|
|
|
+ return getProperty(key, targetType);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("Application property get failed: {}, {}", key, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取环境变量值
|
|
* 获取环境变量值
|
|
*
|
|
*
|
|
@@ -120,6 +200,55 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
return getEnvironment().getProperty(key, targetType, defaultValue);
|
|
return getEnvironment().getProperty(key, targetType, defaultValue);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取安全的环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param defaultValue 默认值
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 环境变量值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getSafeProperty(String key, Class<T> targetType, T defaultValue) {
|
|
|
|
+ try {
|
|
|
|
+ return getProperty(key, targetType, defaultValue);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("Application property get failed: {}, {}", key, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param defaultValue 默认值
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 环境变量值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getProperty(String key, TypeReference<T> targetType, T defaultValue) {
|
|
|
|
+ return ObjectUtils.ifNull(getProperty(key, targetType), defaultValue);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取安全的环境变量值
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param defaultValue 默认值
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 环境变量值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getSafeProperty(String key, TypeReference<T> targetType, T defaultValue) {
|
|
|
|
+ try {
|
|
|
|
+ return getProperty(key, targetType, defaultValue);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("Application property get failed: {}, {}", key, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取必须的环境变量
|
|
* 获取必须的环境变量
|
|
*
|
|
*
|
|
@@ -144,6 +273,19 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
return getEnvironment().getRequiredProperty(key, targetType);
|
|
return getEnvironment().getRequiredProperty(key, targetType);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取必须的环境变量
|
|
|
|
+ *
|
|
|
|
+ * @param key 变量名称
|
|
|
|
+ * @param targetType 变量值类型
|
|
|
|
+ * @param <T> 变量值类型
|
|
|
|
+ * @return 变量值
|
|
|
|
+ * @throws IllegalStateException 状态异常
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getRequiredProperty(String key, TypeReference<T> targetType) throws IllegalStateException {
|
|
|
|
+ return JacksonContextHolder.deserialize(getRequiredProperty(key), targetType);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取对象实例
|
|
* 获取对象实例
|
|
*
|
|
*
|