mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
173 lines
4.7 KiB
Java
173 lines
4.7 KiB
Java
package cn.hutool.extra.spring;
|
||
|
||
import cn.hutool.core.lang.TypeReference;
|
||
import cn.hutool.core.util.ArrayUtil;
|
||
import org.springframework.context.ApplicationContext;
|
||
import org.springframework.context.ApplicationContextAware;
|
||
import org.springframework.context.ConfigurableApplicationContext;
|
||
import org.springframework.core.ResolvableType;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.lang.reflect.ParameterizedType;
|
||
import java.util.Arrays;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* Spring(Spring boot)工具封装,包括:
|
||
*
|
||
* <pre>
|
||
* 1、Spring IOC容器中的bean对象获取
|
||
* </pre>
|
||
*
|
||
* @author loolly
|
||
* @since 5.1.0
|
||
*/
|
||
@Component
|
||
public class SpringUtil implements ApplicationContextAware {
|
||
|
||
private static ApplicationContext applicationContext;
|
||
|
||
@Override
|
||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||
SpringUtil.applicationContext = applicationContext;
|
||
}
|
||
|
||
/**
|
||
* 获取applicationContext
|
||
*
|
||
* @return ApplicationContext
|
||
*/
|
||
public static ApplicationContext getApplicationContext() {
|
||
return applicationContext;
|
||
}
|
||
|
||
//通过name获取 Bean.
|
||
|
||
/**
|
||
* 通过name获取 Bean
|
||
*
|
||
* @param <T> Bean类型
|
||
* @param name Bean名称
|
||
* @return Bean
|
||
*/
|
||
@SuppressWarnings("unchecked")
|
||
public static <T> T getBean(String name) {
|
||
return (T) applicationContext.getBean(name);
|
||
}
|
||
|
||
/**
|
||
* 通过class获取Bean
|
||
*
|
||
* @param <T> Bean类型
|
||
* @param clazz Bean类
|
||
* @return Bean对象
|
||
*/
|
||
public static <T> T getBean(Class<T> clazz) {
|
||
return applicationContext.getBean(clazz);
|
||
}
|
||
|
||
/**
|
||
* 通过name,以及Clazz返回指定的Bean
|
||
*
|
||
* @param <T> bean类型
|
||
* @param name Bean名称
|
||
* @param clazz bean类型
|
||
* @return Bean对象
|
||
*/
|
||
public static <T> T getBean(String name, Class<T> clazz) {
|
||
return applicationContext.getBean(name, clazz);
|
||
}
|
||
|
||
/**
|
||
* 通过类型参考返回带泛型参数的Bean
|
||
*
|
||
* @param reference 类型参考,用于持有转换后的泛型类型
|
||
* @param <T> Bean类型
|
||
* @return 带泛型参数的Bean
|
||
* @since 5.4.0
|
||
*/
|
||
@SuppressWarnings("unchecked")
|
||
public static <T> T getBean(TypeReference<T> reference) {
|
||
final ParameterizedType parameterizedType = (ParameterizedType) reference.getType();
|
||
final Class<T> rawType = (Class<T>) parameterizedType.getRawType();
|
||
final Class<?>[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class<?>) type).toArray(Class[]::new);
|
||
final String[] beanNames = applicationContext.getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes));
|
||
return getBean(beanNames[0], rawType);
|
||
}
|
||
|
||
/**
|
||
* 获取指定类型对应的所有Bean,包括子类
|
||
*
|
||
* @param <T> Bean类型
|
||
* @param type 类、接口,null表示获取所有bean
|
||
* @return 类型对应的bean,key是bean注册的name,value是Bean
|
||
* @since 5.3.3
|
||
*/
|
||
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
|
||
return applicationContext.getBeansOfType(type);
|
||
}
|
||
|
||
/**
|
||
* 获取指定类型对应的Bean名称,包括子类
|
||
*
|
||
* @param type 类、接口,null表示获取所有bean名称
|
||
* @return bean名称
|
||
* @since 5.3.3
|
||
*/
|
||
public static String[] getBeanNamesForType(Class<?> type) {
|
||
return applicationContext.getBeanNamesForType(type);
|
||
}
|
||
|
||
/**
|
||
* 获取配置文件配置项的值
|
||
*
|
||
* @param key 配置项key
|
||
* @return 属性值
|
||
* @since 5.3.3
|
||
*/
|
||
public static String getProperty(String key) {
|
||
return applicationContext.getEnvironment().getProperty(key);
|
||
}
|
||
|
||
/**
|
||
* 获取当前的环境配置,无配置返回null
|
||
*
|
||
* @return 当前的环境配置
|
||
* @since 5.3.3
|
||
*/
|
||
public static String[] getActiveProfiles() {
|
||
return applicationContext.getEnvironment().getActiveProfiles();
|
||
}
|
||
|
||
/**
|
||
* 获取当前的环境配置,当有多个环境配置时,只获取第一个
|
||
*
|
||
* @return 当前的环境配置
|
||
* @since 5.3.3
|
||
*/
|
||
public static String getActiveProfile() {
|
||
final String[] activeProfiles = getActiveProfiles();
|
||
return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
|
||
}
|
||
|
||
/**
|
||
* 动态向Spring注册Bean
|
||
* <p>
|
||
* 由{@link org.springframework.beans.factory.BeanFactory} 实现,通过工具开放API
|
||
*
|
||
* @param beanName 名称
|
||
* @param bean bean
|
||
* @param <T> 泛型
|
||
* @author shadow
|
||
* @date 2020-09-04
|
||
*/
|
||
public static <T> void registerBean(String beanName, T bean) {
|
||
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
|
||
context.getBeanFactory().registerSingleton(beanName, bean);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|