繁体   English   中英

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

[英]Class cannot be cast to java.lang.reflect.ParameterizedType

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

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

org.springframework.beans.factory.BeanCreationException:创建名为'contentController'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.fettergroup.cmt.service.VariableService com.fettergroup.cmt.web.ContentController.variableService; 嵌套异常是org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义创建名为'variableService'的bean时出错:bean的实例化失败; 嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.fettergroup.cmt.service.VariableService]:构造函数抛出异常; 嵌套异常是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<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 {

}

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;
    }
}

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

@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);
    }
}

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];
}

看来你是在滥用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<T>) type;
    } else if (type instanceof ParameterizedType) {
      this.entityClass = (Class<T>) ((ParameterizedType)type).getRawType();
    }
}

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

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

阅读这个主题

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM