Просмотр исходного кода

启动时初始化Spring上下文环境工具类

shuzheng 9 лет назад
Родитель
Сommit
f332818593

+ 36 - 0
common/src/main/java/com/zheng/common/util/SpringContextUtil.java

@@ -0,0 +1,36 @@
+package com.zheng.common.util;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+/**
+ * 资源文件读取工具
+ *
+ * @author shuzheng
+ * @date 2016年10月15日
+ */
+public class SpringContextUtil implements ApplicationContextAware {
+
+	public static SpringContextUtil springContextUtil = new SpringContextUtil();
+
+	private static ApplicationContext context = null;
+
+	public final static synchronized SpringContextUtil getInstance() {
+		return springContextUtil;
+	}
+
+	private SpringContextUtil() {
+		super();
+	}
+
+	@Override
+	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+		context = applicationContext;
+	}
+
+	public static Object getBean(String name) {
+		return context.getBean(name);
+	}
+
+}

+ 3 - 0
common/src/main/resources/applicationContext.xml

@@ -11,4 +11,7 @@
 	<!-- 装载service -->
 	<context:component-scan base-package="**.service" />
 
+	<!-- 启动时初始化Spring上下文环境工具类 -->
+	<bean id="springContextUtil" class="com.zheng.common.util.SpringContextUtil"></bean>
+
 </beans>