类不能转换为java.lang.reflect.ParameterizedType

目前,我的控制器中的VariableService@Autowired

我意识到我可以实现ParameterizedType类来使这个错误消失,但我担心我可能会走向错误的方向。 有没有更好的方法来做到这一点,还是我需要咬紧牙关并实现ParameterizedType的方法?

org.springframework.beans.factory.BeanCreationException:创建名为’contentController’的bean时出错:注入自动连接的依赖项失败; 嵌套exception是org.springframework.beans.factory.BeanCreationException:无法自动assembly字段:private com.fettergroup.cmt.service.VariableService com.fettergroup.cmt.web.ContentController.variableService; 嵌套exception是org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义的名称为’variableService’的bean创建错误:bean的实例化失败; 嵌套exception是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.fettergroup.cmt.service.VariableService]:构造函数抛出exception; 嵌套exception是java.lang.ClassCastException: java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType

可变服务

 public class VariableService extends EntityService { public VariableService () { super.setEntityRepository(new VariableRepository()); } } 

EntityService

 public abstract class EntityService { public EntityRepository entityRepository; public T create(T entity) { return entityRepository.create(entity); } public T update(T entity) { return entityRepository.update(entity); } public void delete(T entity) { entityRepository.delete(entity); } public void setEntityRepository(EntityRepository entityRepository) { this.entityRepository = entityRepository; } } 

变量存储库

 public class VariableRepository extends EntityRepository { } 

EntityRepository

 @Repository public abstract class EntityRepository { //the equivalent of User.class protected Class entityClass; @PersistenceContext(type= PersistenceContextType.TRANSACTION) public EntityManager entityManager; public EntityRepository () { //Get "T" and assign it to this.entityClass ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); this.entityClass = (Class) genericSuperclass.getActualTypeArguments()[0]; } /** * Create this entity * @param t * @return */ public T create(T t) { entityManager.persist(t); return t; } /** * Update this entity * @param t * @return */ public T update(T t) { return entityManager.merge(t); } /** * Delete this entity * @param entity */ public void delete(T t) { t = this.update(t); entityManager.remove(t); } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } } 

只是说,这是一个非常糟糕的设计,用于确定实体类型。你应该做以下事情,而不是依靠reflection来推断它的类def ..它不仅会摆脱那个错误,而且会整体更清洁,在低水平,比reflection更快(不是它真的是一个问题)。

 @Repository public abstract class EntityRepository{ protected Class entityClass; public EntityRepository(Class entityClass){ this.entityClass = entityClass; } //... } public class UserEntityRepository extends EntityRepository{ public UserEntityRepository(){ super(User.class); } } 

EntityRepository不是ParameterizedType类型。 该类仅扩展ParameterizedType。 如果spring代理cglib,这也是可能的

我们需要检查class级的父母

 public EntityRepository () { //Get "T" and assign it to this.entityClass Type genericSuperClass = getClass().getGenericSuperclass(); ParameterizedType parametrizedType = null; while (parametrizedType == null) { if ((genericSuperClass instanceof ParameterizedType)) { parametrizedType = (ParameterizedType) genericSuperClass; } else { genericSuperClass = ((Class) genericSuperClass).getGenericSuperclass(); } } entityClass = (Class) parametrizedType.getActualTypeArguments()[0]; } 

看来你是在滥用ParameterizedType 。 在EntityRepository构造函数中,您应该使用类似下面的内容,并进行适当的检查。

 public EntityRepository () { //Get "T" and assign it to this.entityClass ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); Type type = genericSuperclass.getActualTypeArguments()[0]; if (type instanceof Class) { this.entityClass = (Class) type; } else if (type instanceof ParameterizedType) { this.entityClass = (Class) ((ParameterizedType)type).getRawType(); } } 

但是,这个实现还远未完成,因为您还应该处理GenericArrayType值以及嵌套的ParameterizedType

需要从EntityRepository类中删除@Repository注释以解决问题。

阅读这个post

java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType