简体   繁体   English

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

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

Currently the VariableService is @Autowired in my controller. 目前,我的控制器中的VariableService@Autowired

I realize I can implement the class ParameterizedType to make this error go away but I fear that I may be headed in the wrong direction. 我意识到我可以实现ParameterizedType类来使这个错误消失,但我担心我可能会走向错误的方向。 Is there a better way to do this or do I need to bite the bullet and implement ParameterizedType 's methods? 有没有更好的方法来做到这一点,还是我需要咬紧牙关并实现ParameterizedType的方法?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contentController': Injection of autowired dependencies failed; org.springframework.beans.factory.BeanCreationException:创建名为'contentController'的bean时出错:注入自动连接的依赖项失败; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fettergroup.cmt.service.VariableService com.fettergroup.cmt.web.ContentController.variableService; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.fettergroup.cmt.service.VariableService com.fettergroup.cmt.web.ContentController.variableService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'variableService' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; 嵌套异常是org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义创建名为'variableService'的bean时出错:bean的实例化失败; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.fettergroup.cmt.service.VariableService]: Constructor threw exception; 嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.fettergroup.cmt.service.VariableService]:构造函数抛出异常; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 嵌套异常是java.lang.ClassCastException: java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType

Variable Service 可变服务

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

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

Variable Repository 变量存储库

public class VariableRepository extends EntityRepository {

}

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

Just saying, this is a pretty poor design for determining the entity type.. You should do the following instead of relying on reflection to infer its class def.. Not only will it get rid of that error, but it will be overall cleaner and at a low level, faster than reflection (not that it would really be an issue). 只是说,这是一个非常糟糕的设计,用于确定实体类型。你应该做以下事情,而不是依靠反射来推断它的类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 isn't ParameterizedType type. EntityRepository不是ParameterizedType类型。 This class only extends ParameterizedType. 该类仅扩展ParameterizedType。 This is also possible if spring proxied you class by cglib 如果spring通过cglib代理你,这也是可能的

We need check parents of 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<T>) parametrizedType.getActualTypeArguments()[0];
}

It seems you are misusing ParameterizedType . 看来你是在滥用ParameterizedType In the EntityRepository constructor, you should be using something like the following, with the appropriate checks. 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();
    }
}

This implementation is far from complete though, as you should also handle GenericArrayType values, as well as nested ParameterizedType . 但是,这个实现还远未完成,因为您还应该处理GenericArrayType值以及嵌套的ParameterizedType

@Repository annotation needs to be removed from the class EntityRepository to solve the problem. 需要从EntityRepository类中删除@Repository注释以解决问题。

Read this thread 阅读这个主题

java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType

暂无
暂无

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

相关问题 嵌套类不能转换为java.lang.reflect.ParameterizedType - Nested class cannot be cast to java.lang.reflect.ParameterizedType java.lang.ClassCastException:无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType - java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType ClassCastException:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - ClassCastException:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 设计模式:Lazy Singleton,Generics和Inheritance:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - Design Pattern: Lazy Singleton, Generics and Inheritance: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 无法将类强制转换为ParameterizedType - Class cannot be cast to ParameterizedType class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized - class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized 测试DAO时出错:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法强制转换为java.lang.Class - Error testing DAOs: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class 引起:java.lang.ClassCastException:使用Generic Type时,libcore.reflect.ParameterizedTypeImpl无法强制转换为java.lang.Class - Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type 类不能强制转换为 java.lang.Comparable - Class cannot be cast to java.lang.Comparable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM