¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦å
å«å¯¹åºåç§°çBean对象 |
| | | * |
| | | * @param name Beanåç§° |
| | | * @return å
å«ï¼è¿åtrueï¼å¦åè¿åfalseã |
| | | */ |
| | | public static boolean containsBean(String name) { |
| | | return context.containsBean(name); |
| | | } |
| | | |
| | | /** |
| | | * è·å对åºBeanåç§°çç±»å |
| | | * |
| | | * @param name Beanåç§° |
| | | * @return è¿å对åºçBeanç±»å |
| | | */ |
| | | public static Class<?> getType(String name) { |
| | | return context.getType(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åä¸ä¸æå¯¹è±¡ï¼å¯è¿è¡åç§Springçä¸ä¸ææä½ |
| | | * |
| | | * @return Springä¸ä¸æå¯¹è±¡ |
| | | */ |
| | | public static ApplicationContext getContext() { |
| | | return context; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è·åå½åç¯å¢ |
| | | * |
| | | * @return Profile |
| | | */ |
| | | public static String getActiveProfile() { |
| | | return context.getEnvironment().getActiveProfiles()[0]; |
| | | } |
| | | } |