gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:41:40 25 4
gpt4 key购买 nike

目前 VariableService 在我的 Controller 中是 @Autowired

我知道我可以实现类 ParameterizedType 来消除这个错误,但我担心我可能会走错方向。有没有更好的方法来做到这一点,还是我需要硬着头皮实现 ParameterizedType 的方法?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: 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; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.fettergroup.cmt.service.VariableService]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to 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);
}
}

关于java - 无法将类转换为 java.lang.reflect.ParameterizedType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10589767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com