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


问题内容

目前VariableService@Autowired在我的控制器。

我意识到我可以实现该类ParameterizedType以使该错误消失,但是我担心我可能会走错方向。有没有更好的方法可以做到这一点,或者我需要硬着头皮实施ParameterizedType的方法吗?

org.springframework.beans.factory.BeanCreationException:创建名称为“
contentController”的bean时出错:自动连接的依赖项注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:私有com.fettergroup.cmt.service.VariableService
com.fettergroup.cmt.web.ContentController.variableService;
嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-
INF/dispatcher-
servlet.xml]中定义的名称为’variableService’的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());
    }
}

实体服务

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

问题答案:

只是说,这对于确定实体类型来说是一个很差的设计。您应该执行以下操作,而不是依靠反射来推断其类定义。.这样不仅可以消除该错误,而且可以使整体更清晰,在低水平上,比反射速度要快(这不是一个真正的问题)。

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