From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001 From: sws <15810472099@163.com> Date: 星期六, 26 十一月 2022 16:12:02 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/SpringContextHelper.java | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 86 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/lf/server/helper/SpringContextHelper.java b/src/main/java/com/lf/server/helper/SpringContextHelper.java new file mode 100644 index 0000000..4a2032e --- /dev/null +++ b/src/main/java/com/lf/server/helper/SpringContextHelper.java @@ -0,0 +1,86 @@ +package com.lf.server.helper; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +/** + * Spring涓婁笅鏂囧府鍔╃被 + * @author WWW + */ +@Component +public class SpringContextHelper implements ApplicationContextAware { + private static ApplicationContext context = null; + + /** + * 璁剧疆搴旂敤绋嬪簭涓婁笅鏂� + * + * @param applicationContext + * @throws BeansException + */ + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + + /** + * 鏍规嵁鍚嶇О鑾峰彇Bean + * + * @param name 绫诲悕绉� + * @return + */ + public static <T> T getBean(String name) { + return (T) context.getBean(name); + } + + /** + * 鏍规嵁绫诲瀷鑾峰彇Bean + * + * @param clazz 绫� + * @param <T> 娉涘瀷 + * @return + */ + public static <T> T getBean(Class<T> clazz) { + return context.getBean(clazz); + } + + /** + * 鍒ゆ柇鏄惁鍖呭惈瀵瑰簲鍚嶇О鐨凚ean瀵硅薄 + * + * @param name Bean鍚嶇О + * @return 鍖呭惈锛氳繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse銆� + */ + public static boolean containsBean(String name) { + return context.containsBean(name); + } + + /** + * 鑾峰彇瀵瑰簲Bean鍚嶇О鐨勭被鍨� + * + * @param name Bean鍚嶇О + * @return 杩斿洖瀵瑰簲鐨凚ean绫诲瀷 + */ + public static Class<?> getType(String name) { + return context.getType(name); + } + + /** + * 鑾峰彇涓婁笅鏂囧璞★紝鍙繘琛屽悇绉峉pring鐨勪笂涓嬫枃鎿嶄綔 + * + * @return Spring涓婁笅鏂囧璞� + */ + public static ApplicationContext getContext() { + return context; + } + + + /** + * 鑾峰彇褰撳墠鐜 + * + * @return Profile + */ + public static String getActiveProfile() { + return context.getEnvironment().getActiveProfiles()[0]; + } +} -- Gitblit v1.9.3