错误/异常:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 的解决方法

1、错误/异常图  

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDao' defined in class path resource [spring/beans_common.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.oa.common.dao.impl.BaseDao]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

 

错误/异常描述:反射类型转换失败(nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

一开始我以为是baseDao注入失败/异常,然后我就仔细检查spring的配置文件(检查了N遍),发现一点问题都没有。后来,经过Debug一步一步调试,发现出错位置:BaseDao()构造器,原代码:

 1     private Class<T> clazz;
 2 
 3     //反射机制,获取对应的对象
 4     @SuppressWarnings("unchecked")
 5     public BaseDao() {//构造函数的作用:获取对应的实体类对象
 6         // this——表示当前类(UserDao)
 7         // this.getClass()——当前运行类的字节码(UserDao.class)
 8         // this.getClass().getGenericSuperclass()——当前运行类的父类(BaseDao<T>,以为User为例,那就是BaseDao<User>)
 9         Type type = this.getClass().getGenericSuperclass(); // generic 泛型
10         // 强制转化“参数化类型”
11         ParameterizedType parameterizedType = (ParameterizedType) type;
12         // 参数化类型中可能有多个泛型参数
13         Type[] types = parameterizedType.getActualTypeArguments();
14         // 获取数据的第一个元素(User.class)
15         clazz = (Class<T>) types[0]; // com.oa.shore.entity.User.class 
16     }

说明:我用的是SSH框架(注解版)。JDK-1.8;Tomcat-7.0.96

我用到的jar包:

    

 

2、解决方法    

加个判断,即可解决:

 1   private Class<T> clazz;
 2 
 3   //反射机制,获取对应的对象
 4   @SuppressWarnings("unchecked")
 5   public BaseDao() {//构造函数的作用:获取对应的实体类对象
 6       // this——表示当前类(UserDao)
 7       // this.getClass()——当前运行类的字节码(UserDao.class)
 8       // this.getClass().getGenericSuperclass()——当前运行类的父类(BaseDao<T>,以为User为例,那就是BaseDao<User>)
 9       Type type = this.getClass().getGenericSuperclass(); // generic 泛型
10       if(type instanceof ParameterizedType){
11           // 强制转化“参数化类型”
12           ParameterizedType parameterizedType = (ParameterizedType) type;
13           // 参数化类型中可能有多个泛型参数
14           Type[] types = parameterizedType.getActualTypeArguments();
15           // 获取数据的第一个元素(User.class)
16           clazz = (Class<T>) types[0]; // com.oa.shore.entity.User.class 
17       }
18   }

说明:如果你的SessionFactory是“手动”的形式交给spring容器注入,那么,上面的问题还是解决不了;想要解决此问题,得 extends HibernateDaoSupport 它,让spring容器自动去管理/注入,即可解决此问题。可参考以下BaseDao实现类的全部代码

 1 package com.oa.common.dao.impl;
 2 
 3 import java.lang.reflect.ParameterizedType;
 4 import java.lang.reflect.Type;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 9 import org.springframework.stereotype.Repository;
10 
11 import com.oa.common.dao.IBaseDao;
12 
13 /**
14  * @author DSHORE/2020-2-6
15  *
16  */
17 @Repository("baseDao")
18 public class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T>  {
19     /* //此处交给spring自动管理/注入了(extends HibernateDaoSupport)
20     @Autowired
21     private SessionFactory sessionFactory;
22      
23     public void setSessionFactory(SessionFactory sessionFactory) {
24         this.sessionFactory = sessionFactory;
25     }*/
26     
27     private Class<T> clazz;
28 
29     //反射机制,获取对应的对象
30     @SuppressWarnings("unchecked")
31     public BaseDao() {//构造函数的作用:获取对应的实体类对象
32         // this——表示当前类(UserDao)
33         // this.getClass()——当前运行类的字节码(UserDao.class)
34         // this.getClass().getGenericSuperclass()——当前运行类的父类(BaseDao<T>,以为User为例,那就是BaseDao<User>)
35         Type type = this.getClass().getGenericSuperclass(); // generic 泛型
36         if(type instanceof ParameterizedType){
37             // 强制转化“参数化类型”
38             ParameterizedType parameterizedType = (ParameterizedType) type;
39             // 参数化类型中可能有多个泛型参数
40             Type[] types = parameterizedType.getActualTypeArguments();
41             // 获取数据的第一个元素(User.class)
42             clazz = (Class<T>) types[0]; // com.oa.shore.entity.User.class 
43         }
44     }
45     
46     @Override //新增
47     public int add(T entity) {
48         return (Integer) getHibernateTemplate().save(entity);
49         //return (Integer) sessionFactory.getCurrentSession().save(entity);
50     }
51 
52     @SuppressWarnings("unchecked")
53     @Override //查询所有
54     public List<T> listAll() {
55         //Query query = sessionFactory.getCurrentSession().createQuery("from " + clazz.getSimpleName());
56         Query query = getSession().createQuery("from " + clazz.getSimpleName()); //这里用clazz.getName()也行
57         return query.list();
58     }
59 }

测试结果图:

 

此SSH注解版项目的完整代码(可参考):https://www.cnblogs.com/dshore123/p/12336358.html

 

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/12331195.html

版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2020-02-19 14:34  DSHORE  阅读(28268)  评论(0编辑  收藏  举报