获取泛型的Class类型

来源:互联网 发布:联影 算法怎么样 编辑:程序博客网 时间:2024/06/07 11:53

场景:通用类抽取时。

注意:泛型注入,只有在spring4及以上版本可用。

private Class<T> clazz;public BaseServiceImpl() {// 获取父类 typeType type = this.getClass().getGenericSuperclass();// 强转,以使用获取泛型类型的方法ParameterizedType ptype = (ParameterizedType) type;// 赋值this.clazz = (Class<T>) ptype.getActualTypeArguments()[0];}


0 0