|
| 1 | +package top.cadecode.uniboot.common.core.util; |
| 2 | + |
| 3 | +import cn.hutool.core.util.ArrayUtil; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import org.springframework.beans.BeansException; |
| 6 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 7 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| 8 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 9 | +import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; |
| 10 | +import org.springframework.context.ApplicationContext; |
| 11 | +import org.springframework.context.ApplicationContextAware; |
| 12 | +import org.springframework.context.ConfigurableApplicationContext; |
| 13 | +import org.springframework.core.ResolvableType; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | +import top.cadecode.uniboot.common.core.enums.CommonErrorEnum; |
| 16 | +import top.cadecode.uniboot.common.core.exception.UniException; |
| 17 | + |
| 18 | +import java.lang.reflect.ParameterizedType; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +/** |
| 23 | + * Spring IOC 容器工具 |
| 24 | + * |
| 25 | + * @author Cade Li |
| 26 | + * @since 2023/6/9 |
| 27 | + */ |
| 28 | +@Component |
| 29 | +public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextAware { |
| 30 | + |
| 31 | + /** |
| 32 | + * 使用 @PostConstruct 时,ApplicationContext 还未加载,导致空指针 |
| 33 | + * 注入 ConfigurableListableBeanFactory 操作 bean |
| 34 | + */ |
| 35 | + private static ConfigurableListableBeanFactory BEAN_FACTORY; |
| 36 | + private static ApplicationContext APPLICATION_CONTEXT; |
| 37 | + |
| 38 | + public static ApplicationContext getApplicationContext() { |
| 39 | + return APPLICATION_CONTEXT; |
| 40 | + } |
| 41 | + |
| 42 | + public static ListableBeanFactory getBeanFactory() { |
| 43 | + return null == BEAN_FACTORY ? APPLICATION_CONTEXT : BEAN_FACTORY; |
| 44 | + } |
| 45 | + |
| 46 | + public static ConfigurableListableBeanFactory getConfigurableBeanFactory() { |
| 47 | + final ConfigurableListableBeanFactory factory; |
| 48 | + if (null != BEAN_FACTORY) { |
| 49 | + factory = BEAN_FACTORY; |
| 50 | + } else if (APPLICATION_CONTEXT instanceof ConfigurableApplicationContext) { |
| 51 | + factory = ((ConfigurableApplicationContext) APPLICATION_CONTEXT).getBeanFactory(); |
| 52 | + } else { |
| 53 | + throw UniException.of(CommonErrorEnum.NO_LISTABLE_BEAN_FACTORY, ""); |
| 54 | + } |
| 55 | + return factory; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 通过name获取 Bean |
| 60 | + */ |
| 61 | + @SuppressWarnings("unchecked") |
| 62 | + public static <T> T getBean(String name) { |
| 63 | + return (T) getBeanFactory().getBean(name); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 通过class获取Bean |
| 68 | + */ |
| 69 | + public static <T> T getBean(Class<T> clazz) { |
| 70 | + return getBeanFactory().getBean(clazz); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 通过name,以及Clazz返回指定的Bean |
| 75 | + */ |
| 76 | + public static <T> T getBean(String name, Class<T> clazz) { |
| 77 | + return getBeanFactory().getBean(name, clazz); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * 通过类型参考返回带泛型参数的Bean |
| 82 | + */ |
| 83 | + @SuppressWarnings("unchecked") |
| 84 | + public static <T> T getBean(TypeReference<T> reference) { |
| 85 | + final ParameterizedType parameterizedType = (ParameterizedType) reference.getType(); |
| 86 | + final Class<T> rawType = (Class<T>) parameterizedType.getRawType(); |
| 87 | + final Class<?>[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class<?>) type).toArray(Class[]::new); |
| 88 | + final String[] beanNames = getBeanFactory().getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes)); |
| 89 | + return getBean(beanNames[0], rawType); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 获取指定类型对应的所有Bean,包括子类 |
| 94 | + */ |
| 95 | + public static <T> Map<String, T> getBeansOfType(Class<T> type) { |
| 96 | + return getBeanFactory().getBeansOfType(type); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 获取指定类型对应的Bean名称,包括子类 |
| 101 | + */ |
| 102 | + public static String[] getBeanNamesForType(Class<?> type) { |
| 103 | + return getBeanFactory().getBeanNamesForType(type); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * 获取配置文件配置项的值 |
| 108 | + */ |
| 109 | + public static String getProperty(String key) { |
| 110 | + if (null == APPLICATION_CONTEXT) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + return APPLICATION_CONTEXT.getEnvironment().getProperty(key); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * 获取应用程序名称 |
| 118 | + */ |
| 119 | + public static String getApplicationName() { |
| 120 | + return getProperty("spring.application.name"); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * 获取当前的环境配置,无配置返回null |
| 125 | + */ |
| 126 | + public static String[] getActiveProfiles() { |
| 127 | + if (null == APPLICATION_CONTEXT) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + return APPLICATION_CONTEXT.getEnvironment().getActiveProfiles(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 获取当前的环境配置,当有多个环境配置时,只获取第一个 |
| 135 | + */ |
| 136 | + public static String getActiveProfile() { |
| 137 | + final String[] activeProfiles = getActiveProfiles(); |
| 138 | + return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 动态向Spring注册Bean |
| 143 | + */ |
| 144 | + public static <T> void registerBean(String beanName, T bean) { |
| 145 | + final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory(); |
| 146 | + factory.autowireBean(bean); |
| 147 | + factory.registerSingleton(beanName, bean); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * 注销 bean |
| 152 | + */ |
| 153 | + public static void unregisterBean(String beanName) { |
| 154 | + final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory(); |
| 155 | + if (factory instanceof DefaultSingletonBeanRegistry) { |
| 156 | + DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) factory; |
| 157 | + registry.destroySingleton(beanName); |
| 158 | + } else { |
| 159 | + throw UniException.of(CommonErrorEnum.CAN_NOT_UNREGISTER_BEAN, ""); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * 发布事件 |
| 165 | + */ |
| 166 | + public static void publishEvent(Object event) { |
| 167 | + if (null != APPLICATION_CONTEXT) { |
| 168 | + APPLICATION_CONTEXT.publishEvent(event); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 174 | + BEAN_FACTORY = beanFactory; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void setApplicationContext(ApplicationContext applicationContext) { |
| 179 | + APPLICATION_CONTEXT = applicationContext; |
| 180 | + } |
| 181 | +} |
0 commit comments