Bläddra i källkod

2024 Initialize

shjung 1 år sedan
förälder
incheckning
f9f7595e5b

+ 7 - 7
pom.xml

@@ -37,13 +37,13 @@
     </properties>
 
     <dependencies>
-        <dependency>
-            <groupId>com.tsi</groupId>
-            <artifactId>tsi-common</artifactId>
-            <version>1.0</version>
-            <scope>system</scope>
-            <systemPath>${webapp.lib}/tsi-common.jar</systemPath>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>com.tsi</groupId>-->
+<!--            <artifactId>tsi-common</artifactId>-->
+<!--            <version>1.0</version>-->
+<!--            <scope>system</scope>-->
+<!--            <systemPath>${webapp.lib}/tsi-common.jar</systemPath>-->
+<!--        </dependency>-->
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 34 - 0
src/main/java/com/tsi/app/common/app/AppContextProvider.java

@@ -0,0 +1,34 @@
+package com.tsi.app.common.app;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Component;
+
+@Component
+public class AppContextProvider implements ApplicationContextAware {
+    private static ApplicationContext applicationContext;
+    private static String applicationId;
+    private static Environment environment;
+
+    private AppContextProvider() {
+    }
+
+    @Override
+    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
+        applicationContext = ctx;
+        applicationId = ctx.getId();
+        environment = ctx.getEnvironment();
+    }
+
+    public static ApplicationContext getApplicationContext() {
+        return applicationContext;
+    }
+    public static String getApplicationId() {
+        return applicationId;
+    }
+    public static Environment getApplicationEnvironment() {
+        return environment;
+    }
+}

+ 29 - 0
src/main/java/com/tsi/app/common/app/AppUtils.java

@@ -0,0 +1,29 @@
+package com.tsi.app.common.app;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.core.env.Environment;
+
+public final class AppUtils {
+    private AppUtils() {
+    }
+
+    public static Object getBean(Class<?> classType) {
+        ApplicationContext applicationContext = AppContextProvider.getApplicationContext();
+        return applicationContext.getBean(classType);
+    }
+
+    public static Object getBean(String beanName) {
+        ApplicationContext applicationContext = AppContextProvider.getApplicationContext();
+        return applicationContext.getBean(beanName);
+    }
+
+    public static ApplicationContext getApplicationContext() {
+        return AppContextProvider.getApplicationContext();
+    }
+    public static String getApplicationId() {
+        return AppContextProvider.getApplicationId();
+    }
+    public static Environment getApplicationEnvironment() {
+        return AppContextProvider.getApplicationEnvironment();
+    }
+}