!341 SpringUtil 工具在 @PostConstruct 注解标注的方法下失效

Merge pull request !341 from 疯狂的狮子Li/v5-dev
This commit is contained in:
Looly 2021-06-13 23:08:03 +08:00 committed by Gitee
commit 5fe634935d

View File

@ -2,6 +2,9 @@ package cn.hutool.extra.spring;
import cn.hutool.core.lang.TypeReference; import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ArrayUtil; 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.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -23,10 +26,20 @@ import java.util.Map;
* @since 5.1.0 * @since 5.1.0
*/ */
@Component @Component
public class SpringUtil implements ApplicationContextAware { public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextAware {
/**
* Spring应用上下文环境
*/
private static ConfigurableListableBeanFactory beanFactory;
private static ApplicationContext applicationContext; private static ApplicationContext applicationContext;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
SpringUtil.beanFactory = beanFactory;
}
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) { public void setApplicationContext(ApplicationContext applicationContext) {
SpringUtil.applicationContext = applicationContext; SpringUtil.applicationContext = applicationContext;
@ -52,7 +65,7 @@ public class SpringUtil implements ApplicationContextAware {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T getBean(String name) { 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对象 * @return Bean对象
*/ */
public static <T> T getBean(Class<T> clazz) { 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对象 * @return Bean对象
*/ */
public static <T> T getBean(String name, Class<T> clazz) { 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 ParameterizedType parameterizedType = (ParameterizedType) reference.getType();
final Class<T> rawType = (Class<T>) parameterizedType.getRawType(); final Class<T> rawType = (Class<T>) parameterizedType.getRawType();
final Class<?>[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class<?>) type).toArray(Class[]::new); 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); return getBean(beanNames[0], rawType);
} }
@ -104,7 +117,7 @@ public class SpringUtil implements ApplicationContextAware {
* @since 5.3.3 * @since 5.3.3
*/ */
public static <T> Map<String, T> getBeansOfType(Class<T> type) { 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 * @since 5.3.3
*/ */
public static String[] getBeanNamesForType(Class<?> type) { 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 * @since 5.4.2
*/ */
public static <T> void registerBean(String beanName, T bean) { public static <T> void registerBean(String beanName, T bean) {
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext; beanFactory.registerSingleton(beanName, bean);
context.getBeanFactory().registerSingleton(beanName, bean);
} }
} }