¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.yssh.utils; |
| | | |
| | | import org.springframework.aop.framework.AopContext; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| | | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | @Component |
| | | public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware { |
| | | /** Springåºç¨ä¸ä¸æç¯å¢ */ |
| | | private static ConfigurableListableBeanFactory beanFactory; |
| | | |
| | | private static ApplicationContext applicationContext; |
| | | |
| | | @Override |
| | | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| | | SpringUtils.beanFactory = beanFactory; |
| | | } |
| | | |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| | | SpringUtils.applicationContext = applicationContext; |
| | | } |
| | | |
| | | /** |
| | | * è·å对象 |
| | | * |
| | | * @param name |
| | | * @return Object ä¸ä¸ªä»¥æç»ååæ³¨åçbeançå®ä¾ |
| | | * @throws org.springframework.beans.BeansException |
| | | * |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getBean(String name) throws BeansException { |
| | | return (T) beanFactory.getBean(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åç±»å为requiredTypeç对象 |
| | | * |
| | | * @param clz |
| | | * @return |
| | | * @throws org.springframework.beans.BeansException |
| | | * |
| | | */ |
| | | public static <T> T getBean(Class<T> clz) throws BeansException { |
| | | T result = (T) beanFactory.getBean(clz); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 妿BeanFactoryå
å«ä¸ä¸ªä¸æç»åç§°å¹é
çbeanå®ä¹ï¼åè¿åtrue |
| | | * |
| | | * @param name |
| | | * @return boolean |
| | | */ |
| | | public static boolean containsBean(String name) { |
| | | return beanFactory.containsBean(name); |
| | | } |
| | | |
| | | /** |
| | | * 夿以ç»å®ååæ³¨åçbeanå®ä¹æ¯ä¸ä¸ªsingletonè¿æ¯ä¸ä¸ªprototypeã |
| | | * 妿ä¸ç»å®ååç¸åºçbeanå®ä¹æ²¡æè¢«æ¾å°ï¼å°ä¼æåºä¸ä¸ªå¼å¸¸ï¼NoSuchBeanDefinitionExceptionï¼ |
| | | * |
| | | * @param name |
| | | * @return boolean |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.isSingleton(name); |
| | | } |
| | | |
| | | /** |
| | | * @param name |
| | | * @return Class 注å对象çç±»å |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.getType(name); |
| | | } |
| | | |
| | | /** |
| | | * 妿ç»å®çbeanååå¨beanå®ä¹ä¸æå«åï¼åè¿åè¿äºå«å |
| | | * |
| | | * @param name |
| | | * @return |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.getAliases(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åaop代ç对象 |
| | | * |
| | | * @param invoker |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getAopProxy(T invoker) { |
| | | return (T) AopContext.currentProxy(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå½åçç¯å¢é
ç½®ï¼æ é
ç½®è¿ånull |
| | | * |
| | | * @return å½åçç¯å¢é
ç½® |
| | | */ |
| | | public static String[] getActiveProfiles() { |
| | | return applicationContext.getEnvironment().getActiveProfiles(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå½åçç¯å¢é
ç½®ï¼å½æå¤ä¸ªç¯å¢é
ç½®æ¶ï¼åªè·å第ä¸ä¸ª |
| | | * |
| | | * @return å½åçç¯å¢é
ç½® |
| | | */ |
| | | public static String getActiveProfile() { |
| | | final String[] activeProfiles = getActiveProfiles(); |
| | | return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; |
| | | } |
| | | } |