feat: 增加spring上下文

This commit is contained in:
wangyu 2021-07-10 20:32:02 +08:00
parent 6036548f45
commit 2b7a89fbf8
1 changed files with 14 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package com.flyfish.framework.context; package com.flyfish.framework.context;
import com.flyfish.framework.utils.Assert;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -16,6 +17,12 @@ public class SpringContext implements ApplicationContextAware {
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
private static SpringContext INSTANCE;
public SpringContext() {
INSTANCE = this;
}
/** /**
* 设置上下文 * 设置上下文
* *
@ -34,7 +41,12 @@ public class SpringContext implements ApplicationContextAware {
* @param <T> 泛型 * @param <T> 泛型
* @return 结果 * @return 结果
*/ */
public <T> T getBean(Class<T> clazz) { public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz); return getInstance().applicationContext.getBean(clazz);
}
private static SpringContext getInstance() {
Assert.notNull(INSTANCE, "spring工具类初始化失败请检查该bean是否注入");
return INSTANCE;
} }
} }