类spring ioc 泛型保留

类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 中解决泛型问题

上一篇:SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)


下一篇:Word,MathType与LaTeX公式