Merge pull request #1009 from dejavuhuh/v5-dev

[新特性] SpringUtil.getBean(TypeReference<T>)
This commit is contained in:
Golden Looly 2020-08-10 09:56:24 +08:00 committed by GitHub
commit cc522903b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -1,10 +1,14 @@
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.core.ResolvableType;
import org.springframework.stereotype.Component;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.Map;
/**
@ -73,6 +77,22 @@ public class SpringUtil implements ApplicationContextAware {
return applicationContext.getBean(name, clazz);
}
/**
* 通过类型参考返回带泛型参数的Bean
*
* @param reference 类型参考用于持有转换后的泛型类型
* @param <T> Bean类型
* @return 带泛型参数的Bean
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(TypeReference<T> reference) {
ParameterizedType parameterizedType = (ParameterizedType) reference.getType();
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 getBean(beanNames[0], rawType);
}
/**
* 获取指定类型对应的所有Bean包括子类
*

View File

@ -1,5 +1,7 @@
package cn.hutool.extra.spring;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
@ -8,6 +10,9 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {SpringUtil.class, SpringUtilTest.Demo2.class})
//@Import(cn.hutool.extra.spring.SpringUtil.class)
@ -20,6 +25,14 @@ public class SpringUtilTest {
Assert.assertEquals("test", testDemo.getName());
}
@Test
public void getBeanWithTypeReferenceTest() {
Map<String, Object> mapBean = SpringUtil.getBean(new TypeReference<Map<String, Object>>() {});
Assert.assertNotNull(mapBean);
Assert.assertEquals("value1", mapBean.get("key1"));
Assert.assertEquals("value2", mapBean.get("key2"));
}
@Data
public static class Demo2{
private long id;
@ -32,5 +45,13 @@ public class SpringUtilTest {
demo.setName("test");
return demo;
}
@Bean(name="mapDemo")
public Map<String, Object> generateMap() {
HashMap<String, Object> map = MapUtil.newHashMap();
map.put("key1", "value1");
map.put("key2", "value2");
return map;
}
}
}