当前位置: 首页 > 知识库问答 >
问题:

类不能强制转换为Java . lang . reflect . parameterized type

段干靖
2023-03-14

目前,变量服务在我的控制器中@Autowired

我意识到我可以实现类<code>ParameteredType

org.springframework.beans.factory。BeanCreationException:创建名为“contentController”的bean时出错:注入自动连线依赖项失败;嵌套异常是org.springframework.beans.factory。BeanCreationException:无法自动连接字段:private com.fettergroup.cmt.service。VariableService是com.fettergroup.cmt.web.ContentController。可变服务;嵌套异常是org.springframework.beans.factory。BeanCreationException:创建名为“variableService”的bean时出错,该bean在ServletContext资源[WEB-INF/DispatcherServlet.xml]中定义:bean实例化失败;嵌套异常是org.springframework.bean。BeanInstantiationException:无法实例化bean类[com.fettergroup.cmt.service.VariableService]:构造函数引发了异常;嵌套异常为java.lang.ClassCastException:java.lang.Class不能强制转换为java.lang.reflect.ParameteredType

可变服务

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

实体服务

public abstract class EntityService<T> {

    public EntityRepository<T> 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<T> entityRepository) {
        this.entityRepository = entityRepository;
    }
}

变量存储库

public class VariableRepository extends EntityRepository {

}

实体存储库

@Repository
public abstract class EntityRepository<T> {

    //the equivalent of User.class
    protected Class<T> 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<T>) 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;
    }
}

共有3个答案

萧嘉禧
2023-03-14

似乎您误用了参数化类型。在<code>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<T>) type;
    } else if (type instanceof ParameterizedType) {
      this.entityClass = (Class<T>) ((ParameterizedType)type).getRawType();
    }
}

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

东门翰
2023-03-14

EntityRepository不是ParameterizedType类型。此类仅扩展ParameterizedType。如果Spring通过cglib代理您的类,这也是可能的

我们需要检查班级的父母

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<T>) parametrizedType.getActualTypeArguments()[0];
}
王念
2023-03-14

只是说,这对于确定实体类型来说是一个非常糟糕的设计。您应该执行以下操作,而不是依赖反射来推断其类定义。它不仅会消除该错误,而且总体上会更干净,在较低的级别上,比反射更快(这并不是一个真正的问题)。

@Repository
public abstract class EntityRepository<T>{
    protected Class<T> entityClass;

    public EntityRepository(Class<T> entityClass){
        this.entityClass = entityClass;
    }

    //...
}


public class UserEntityRepository extends EntityRepository<User>{
    public UserEntityRepository(){
        super(User.class);
    }
}
 类似资料: