|
|
@@ -0,0 +1,72 @@
|
|
|
+package com.uas.ps.message.util;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationEvent;
|
|
|
+
|
|
|
+/**
|
|
|
+ * spring容器上下文对象
|
|
|
+ *
|
|
|
+ * @author yingp
|
|
|
+ */
|
|
|
+public class ContextUtils {
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(ContextUtils.class);
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setApplicationContext(ApplicationContext applicationContext) {
|
|
|
+ synchronized (ContextUtils.class) {
|
|
|
+ logger.debug("setApplicationContext, notifyAll");
|
|
|
+ ContextUtils.applicationContext = applicationContext;
|
|
|
+ ContextUtils.class.notifyAll();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|