類spring ioc 泛型保留 什麼是泛型擦除 Java並不會傳遞泛型類,舉個直觀的慄子: 這裡 嘗試列印泛型類型, 泛型指定了 類,來個測試看看 是否能被獲取到? 依賴腳本build.gradle 運行可以看到結果是,spring ioc並不能註入獲取泛型 自定義IOC泛型註入 在解決sprin ...
類spring ioc 泛型保留
什麼是泛型擦除
Java並不會傳遞泛型類,舉個直觀的慄子:
@Component
public class BaseProvider<T>
{
public void doSomething()
{
Class<?> clazz = getClass();
System.out.println(clazz.getName()+" doSomething");
Type superclass = clazz.getGenericSuperclass();
System.out.println("superclass="+superclass);
}
}
這裡doSomething
嘗試列印泛型類型,
@Component
public class TestService
{
@Autowired
private BaseProvider<User> userProvdier;
public void doSomething()
{
userProvdier.doSomething();
}
}
BaseProvider<User>
泛型指定了User
類,來個測試看看User
是否能被獲取到?
@ComponentScan
public class Main
{
public static void main(String[] args)
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Main.class);
TestService service = context.getBean(TestService.class);
System.out.println("service=" + service);
service.doSomething();
}
}
依賴腳本build.gradle
dependencies {
String springVersion="5.1.9.RELEASE"
implementation "org.springframework:spring-context:$springVersion"
}
運行可以看到結果是,spring ioc並不能註入獲取泛型
service=TestService@a68df9
BaseProvider doSomething
superclass=class java.lang.Object
自定義IOC泛型註入
在解決spring泛型問題之前,先做一個簡單的IOC泛型註入框架,來瞭解其原理
TestService service = Context.getBean(TestService.class);
}
這裡將自寫一個Context
類,理解為上下文也好,BeanFactory也好,其原理不過是創建管理對象的東西
public class Context
{
public static <T> T getBean(Class<T> clazz)
{
return createBean(clazz, null, null, null);
}
private static <T> T createBean(Class<T> clazz, Type type, Object target, Field source)
{
//...
}
//....
}
設計createBean
創建bean對象,依賴於bean類型,泛型類型,上級調用對象,來源位置(欄位或方法)。再一個緩存bean對象,基本功能:
private static final Map<String, Object> beanCache = new HashMap<>();
@SuppressWarnings("unchecked")
private static <T> T createBean(Class<T> clazz, Type type, Object target, Member source)
{
try
{
String beanName = getBeanName(clazz, type, target, source);
if (beanCache.containsKey(beanName))
{
return (T) beanCache.get(beanName);
}
if (type != null && type instanceof ParameterizedType)
{
//創建泛型對象代理類
}
Constructor<T> cts = clazz.getDeclaredConstructor();
T obj = cts.newInstance();// 創建object
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields)
{
if (field.getAnnotation(Autowried.class) != null)
{
setField(field, obj, createBean(field.getType(), field.getGenericType(), obj, field));
}
}
beanCache.put(beanName, obj);
return obj;
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static String getBeanName(Class<?> clazz, Type type, Object target, Member source)
{
if (type != null && type instanceof ParameterizedType)
{
ParameterizedType pt = (ParameterizedType) type;
StringJoiner sb = new StringJoiner("_");
for (Type p : pt.getActualTypeArguments())
{
sb.add(p.getTypeName().replaceAll("\\.", "_"));
}
return clazz.getName() + "_$_" + sb.toString() + "_Proxy";
}
return clazz.getName();
}
getBeanName
規定了beanname的生成規則,createBean
中創建泛型代理的部分,這裡使用javassist去生成動態代理類
implementation 'org.javassist:javassist:3.25.0-GA'
生成泛型子類
ClassPool pool = ClassPool.getDefault();
ParameterizedType pt = (ParameterizedType) type;
CtClass subClass = pool.get(clazz.getName());
StringJoiner genericSignature=new StringJoiner(",","L"+getClassPath(subClass.getName())+"<",">;");
Type[] ptts = pt.getActualTypeArguments();
for(int i=0;i<ptts.length;i++) {
genericSignature.add("L"+getClassPath(ptts[i].getTypeName())+";");
}
CtClass proxyClass = pool.makeClass( beanName,subClass);
proxyClass.setGenericSignature( genericSignature.toString());
clazz = (Class<T>) proxyClass.toClass();
getClassPath
替換classname為class路徑
private static String getClassPath(String name)
{
return name.replaceAll("\\.", "/");
}
再來看看運行效果
service=TestService@5d6f64b1
BaseProvider_$_User_Proxy doSomething
superclass=BaseProvider<User>
泛型<User>
能夠獲取出來
為什麼要泛型註入
泛型註入的應用場景,Java獲取泛型一直是個很頭疼的是,如果能夠輕鬆獲取泛型,就能夠減少大量的代碼。舉個慄子,寫一個常用的資料庫操作工具類,不能獲取泛型情況下,就必須傳入Class<T> beanClazz
參數:
public class DbUtil<T>{
List<T> getAll(Class<T> beanClazz){
//....
}
}
思考
方法的泛型應該如何去獲取?
預告
將在下篇文章中講解如何在spring 中解決泛型問題