mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Add: SpringUtil.getBean(TypeReference<T>)
添加Spring工具类静态方法,通过类型参考(TypeReference)获取带泛型参数的Bean,同时添加了一个小的单元测试
This commit is contained in:
parent
741c0cfe1a
commit
04dd1d67d5
@ -1,6 +1,5 @@
|
||||
package cn.hutool.extra.spring;
|
||||
|
||||
import cn.hutool.core.lang.ParameterizedTypeImpl;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@ -9,7 +8,6 @@ import org.springframework.core.ResolvableType;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
@ -92,7 +90,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
Class<T> rawType = (Class<T>) parameterizedType.getRawType();
|
||||
Class<?>[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class<?>) type).toArray(Class[]::new);
|
||||
String[] beanNames = applicationContext.getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes));
|
||||
return applicationContext.getBean(beanNames[0], rawType);
|
||||
return getBean(beanNames[0], rawType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,9 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
|
Loading…
x
Reference in New Issue
Block a user