|
@@ -0,0 +1,43 @@
|
|
|
+package com.usky.dxtop.common.utils.spring;
|
|
|
+
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yq
|
|
|
+ * @date 2021/8/27 13:43
|
|
|
+ */
|
|
|
+public class SpringEnvUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前运行的profile,如果有多个profile,则取第一个
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getProfile() {
|
|
|
+ return Optional.ofNullable(SpringUtils.getApplicationContext())
|
|
|
+ .map(c -> c.getEnvironment())
|
|
|
+ .map(m -> m.getActiveProfiles())
|
|
|
+ .filter(p -> p.length > 0)
|
|
|
+ .map(p -> p[0]).orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前运行的所有profile
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String[] getProfiles() {
|
|
|
+ return Optional.ofNullable(SpringUtils.getApplicationContext())
|
|
|
+ .map(c -> c.getEnvironment())
|
|
|
+ .map(m -> m.getActiveProfiles())
|
|
|
+ .filter(p -> p.length > 0)
|
|
|
+ .orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getProper(String key, String defaultValue) {
|
|
|
+ return Optional.ofNullable(SpringUtils.getApplicationContext())
|
|
|
+ .map(c -> c.getEnvironment())
|
|
|
+ .map(m -> m.getProperty(key, defaultValue))
|
|
|
+ .orElse(null);
|
|
|
+ }
|
|
|
+}
|