|
|
@@ -0,0 +1,78 @@
|
|
|
+package cn.hutool.extra.spring;
|
|
|
+
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Spring(Spring boot)工具封装,包括:
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * 1、Spring IOC容器中的bean对象获取
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author loolly
|
|
|
+ * @since 5.1.0
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class SpringUtil implements ApplicationContextAware {
|
|
|
+
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ @SuppressWarnings("NullableProblems")
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) {
|
|
|
+ if (SpringUtil.applicationContext == null) {
|
|
|
+ SpringUtil.applicationContext = applicationContext;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取applicationContext
|
|
|
+ *
|
|
|
+ * @return ApplicationContext
|
|
|
+ */
|
|
|
+ public static ApplicationContext getApplicationContext() {
|
|
|
+ return applicationContext;
|
|
|
+ }
|
|
|
+
|
|
|
+ //通过name获取 Bean.
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过name获取 Bean
|
|
|
+ * @param name Bean名称
|
|
|
+ * @return Bean
|
|
|
+ */
|
|
|
+ public static <T> T getBean(String name) {
|
|
|
+ //noinspection unchecked
|
|
|
+ return (T) applicationContext.getBean(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过class获取Bean
|
|
|
+ *
|
|
|
+ * @param <T> Bean类型
|
|
|
+ * @param clazz Bean类
|
|
|
+ * @return Bean对象
|
|
|
+ */
|
|
|
+ public static <T> T getBean(Class<T> clazz) {
|
|
|
+ return applicationContext.getBean(clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过name,以及Clazz返回指定的Bean
|
|
|
+ *
|
|
|
+ * @param <T> bean类型
|
|
|
+ * @param name Bean名称
|
|
|
+ * @param clazz bean类型
|
|
|
+ * @return Bean对象
|
|
|
+ */
|
|
|
+ public static <T> T getBean(String name, Class<T> clazz) {
|
|
|
+ return applicationContext.getBean(name, clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|