mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
!341 SpringUtil 工具在 @PostConstruct
注解标注的方法下失效
Merge pull request !341 from 疯狂的狮子Li/v5-dev
This commit is contained in:
commit
5fe634935d
@ -2,6 +2,9 @@ package cn.hutool.extra.spring;
|
||||
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import org.springframework.beans.BeansException;
|
||||
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.context.ConfigurableApplicationContext;
|
||||
@ -23,10 +26,20 @@ import java.util.Map;
|
||||
* @since 5.1.0
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements ApplicationContextAware {
|
||||
public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextAware {
|
||||
|
||||
/**
|
||||
* Spring应用上下文环境
|
||||
*/
|
||||
private static ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
SpringUtil.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
@ -52,7 +65,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(String name) {
|
||||
return (T) applicationContext.getBean(name);
|
||||
return (T) beanFactory.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,7 +76,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
* @return Bean对象
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
return beanFactory.getBean(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,7 +88,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
* @return Bean对象
|
||||
*/
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return applicationContext.getBean(name, clazz);
|
||||
return beanFactory.getBean(name, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +104,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
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));
|
||||
final String[] beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes));
|
||||
return getBean(beanNames[0], rawType);
|
||||
}
|
||||
|
||||
@ -104,7 +117,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
|
||||
return applicationContext.getBeansOfType(type);
|
||||
return beanFactory.getBeansOfType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,7 +128,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static String[] getBeanNamesForType(Class<?> type) {
|
||||
return applicationContext.getBeanNamesForType(type);
|
||||
return beanFactory.getBeanNamesForType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,8 +175,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
* @since 5.4.2
|
||||
*/
|
||||
public static <T> void registerBean(String beanName, T bean) {
|
||||
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
|
||||
context.getBeanFactory().registerSingleton(beanName, bean);
|
||||
beanFactory.registerSingleton(beanName, bean);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user