From 6036548f45026e951ab1826e5d6b7d051cc751a2 Mon Sep 17 00:00:00 2001 From: wangyu <727842003@qq.com> Date: Sat, 26 Jun 2021 23:10:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0spring=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/context/SpringContext.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 flyfish-common/src/main/java/com/flyfish/framework/context/SpringContext.java diff --git a/flyfish-common/src/main/java/com/flyfish/framework/context/SpringContext.java b/flyfish-common/src/main/java/com/flyfish/framework/context/SpringContext.java new file mode 100644 index 0000000..003b215 --- /dev/null +++ b/flyfish-common/src/main/java/com/flyfish/framework/context/SpringContext.java @@ -0,0 +1,40 @@ +package com.flyfish.framework.context; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +/** + * spring的上下文 + * + * @author wangyu + */ +@Component +public class SpringContext implements ApplicationContextAware { + + private ApplicationContext applicationContext; + + /** + * 设置上下文 + * + * @see BeanInitializationException + */ + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + /** + * 通过类获取某个bean + * 优先获取@Prmary注解的bean + * + * @param clazz 类 + * @param 泛型 + * @return 结果 + */ + public T getBean(Class clazz) { + return applicationContext.getBean(clazz); + } +}