本文介绍了尽管Java的类型擦除,但获得T.class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从一个配置文件中读取一个接口到它的实现,以便将它提供给我的IoC容器。这大致是我想要做的:
I'm trying to bind an interface to its implementation as read from a configuration file so that I can feed it to my IoC container. Here's roughly what I'm trying to do:
public class PropertyImplementationBinder<T> {
// ...
public Class getInterfaceClass() {
return T.class; // OR Class<T>, note T is not newable
}
public Class getImplementationClass() {
return /* read config file to get implementation class */;
}
}
是否有可能获得 T.class
?
推荐答案
private final Class<T> clazz;
PropertyImplementationBinder(Class<T> clazz){
this.clazz = clazz;
}
public Class<T> getInterfaceClass() {
return clazz;
}
这篇关于尽管Java的类型擦除,但获得T.class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!