目前我的控制器中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;
}
}
答案 0 :(得分:6)
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];
}
答案 1 :(得分:2)
您似乎在滥用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
。
答案 2 :(得分:1)
需要从EntityRepository类中删除@Repository注释来解决问题。
阅读此主题
java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType