|
@@ -1,77 +1,77 @@
|
|
|
-package com.uas.search.console.b2b.core.util;
|
|
|
-
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
-import org.springframework.context.ApplicationEvent;
|
|
|
-import org.springframework.context.ApplicationListener;
|
|
|
-
|
|
|
-/**
|
|
|
- * spring容器上下文对象
|
|
|
- *
|
|
|
- * @author yingp
|
|
|
- *
|
|
|
- */
|
|
|
-public class ContextUtils implements ApplicationListener<ApplicationPreparedEvent> {
|
|
|
-
|
|
|
- private static ApplicationContext applicationContext;
|
|
|
-
|
|
|
- private static Logger logger = LoggerFactory.getLogger(ContextUtils.class);
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onApplicationEvent(ApplicationPreparedEvent event) {
|
|
|
- synchronized (ContextUtils.class) {
|
|
|
- logger.debug("setApplicationContext, notifyAll");
|
|
|
- ContextUtils.applicationContext = event.getApplicationContext();
|
|
|
- ContextUtils.class.notifyAll();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static ApplicationContext getApplicationContext() {
|
|
|
- synchronized (ContextUtils.class) {
|
|
|
- while (applicationContext == null) {
|
|
|
- try {
|
|
|
- logger.debug("getApplicationContext, wait...");
|
|
|
- ContextUtils.class.wait(60000);
|
|
|
- if (applicationContext == null) {
|
|
|
- logger.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
|
|
|
- }
|
|
|
- } catch (InterruptedException ex) {
|
|
|
- logger.debug("getApplicationContext, wait interrupted");
|
|
|
- }
|
|
|
- }
|
|
|
- return applicationContext;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取bean
|
|
|
- *
|
|
|
- * @param name
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Object getBean(String name) {
|
|
|
- return getApplicationContext().getBean(name);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取bean
|
|
|
- *
|
|
|
- * @param cls
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> T getBean(Class<T> cls) {
|
|
|
- return getApplicationContext().getBean(cls);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 触发事件
|
|
|
- *
|
|
|
- * @param event
|
|
|
- */
|
|
|
- public static void publishEvent(ApplicationEvent event) {
|
|
|
- getApplicationContext().publishEvent(event);
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package com.uas.search.console.b2b.util;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationEvent;
|
|
|
+import org.springframework.context.ApplicationListener;
|
|
|
+
|
|
|
+/**
|
|
|
+ * spring容器上下文对象
|
|
|
+ *
|
|
|
+ * @author yingp
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ContextUtils implements ApplicationListener<ApplicationPreparedEvent> {
|
|
|
+
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(ContextUtils.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onApplicationEvent(ApplicationPreparedEvent event) {
|
|
|
+ synchronized (ContextUtils.class) {
|
|
|
+ logger.debug("setApplicationContext, notifyAll");
|
|
|
+ ContextUtils.applicationContext = event.getApplicationContext();
|
|
|
+ ContextUtils.class.notifyAll();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ApplicationContext getApplicationContext() {
|
|
|
+ synchronized (ContextUtils.class) {
|
|
|
+ while (applicationContext == null) {
|
|
|
+ try {
|
|
|
+ logger.debug("getApplicationContext, wait...");
|
|
|
+ ContextUtils.class.wait(60000);
|
|
|
+ if (applicationContext == null) {
|
|
|
+ logger.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
|
|
|
+ }
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
+ logger.debug("getApplicationContext, wait interrupted");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return applicationContext;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取bean
|
|
|
+ *
|
|
|
+ * @param name
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Object getBean(String name) {
|
|
|
+ return getApplicationContext().getBean(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取bean
|
|
|
+ *
|
|
|
+ * @param cls
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> T getBean(Class<T> cls) {
|
|
|
+ return getApplicationContext().getBean(cls);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 触发事件
|
|
|
+ *
|
|
|
+ * @param event
|
|
|
+ */
|
|
|
+ public static void publishEvent(ApplicationEvent event) {
|
|
|
+ getApplicationContext().publishEvent(event);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|