| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.aop.framework.AopContext; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * spring工具类 方便在非spring管理环境中获取bean |
| | | * |
| | |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware |
| | | { |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 获取对象 |
| | |
| | | * |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getBean(String name) throws BeansException |
| | | { |
| | | return (T) beanFactory.getBean(name); |
| | | public static <T> T getBean(String name) throws BeansException { |
| | | return (T)beanFactory.getBean(name); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws org.springframework.beans.BeansException |
| | | * |
| | | */ |
| | | public static <T> T getBean(Class<T> clz) throws BeansException |
| | | { |
| | | T result = (T) beanFactory.getBean(clz); |
| | | public static <T> T getBean(Class<T> clz) throws BeansException { |
| | | T result = beanFactory.getBean(clz); |
| | | return result; |
| | | } |
| | | |
| | |
| | | * @param name |
| | | * @return boolean |
| | | */ |
| | | public static boolean containsBean(String name) |
| | | { |
| | | public static boolean containsBean(String name) { |
| | | return beanFactory.containsBean(name); |
| | | } |
| | | |
| | |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.isSingleton(name); |
| | | } |
| | | |
| | |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.getType(name); |
| | | } |
| | | |
| | |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static String[] getAliases(String name) throws 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(); |
| | | public static <T> T getAopProxy(T invoker) { |
| | | return (T)AopContext.currentProxy(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return 当前的环境配置 |
| | | */ |
| | | public static String[] getActiveProfiles() |
| | | { |
| | | public static String[] getActiveProfiles() { |
| | | return applicationContext.getEnvironment().getActiveProfiles(); |
| | | } |
| | | |
| | |
| | | * |
| | | * @return 当前的环境配置 |
| | | */ |
| | | public static String getActiveProfile() |
| | | { |
| | | public static String getActiveProfile() { |
| | | final String[] activeProfiles = getActiveProfiles(); |
| | | return !ObjectUtils.isEmpty(activeProfiles) ? activeProfiles[0] : null; |
| | | } |
| | | |
| | | @Override |
| | | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| | | SpringUtils.beanFactory = beanFactory; |
| | | } |
| | | |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| | | SpringUtils.applicationContext = applicationContext; |
| | | } |
| | | } |