簡體   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