Browse Source

增加ApplicationContextHolder

zhouhao 8 years ago
parent
commit
8225290462

+ 6 - 0
hsweb-commons/hsweb-commons-utils/pom.xml

@@ -38,6 +38,12 @@
             <optional>true</optional>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <optional>true</optional>
+        </dependency>
+
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>

+ 28 - 0
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/ApplicationContextHolder.java

@@ -0,0 +1,28 @@
+package org.hswebframework.web;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.stereotype.Component;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+@Component
+public class ApplicationContextHolder {
+    private static ApplicationContext context;
+
+    public static final ApplicationContext get() {
+        if (null == context) {
+            throw new UnsupportedOperationException("ApplicationContext not ready!");
+        }
+        return context;
+    }
+
+    @Autowired
+    public void setContext(ApplicationContext context) {
+        if (null == ApplicationContextHolder.context)
+            ApplicationContextHolder.context = context;
+    }
+}