我有一个通用的 dao 类,我正在尝试使用 spring 依赖注入(inject),但出现以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BaseDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.abgc.fab.dao.BaseDao]: Constructor threw exception; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
我的applicationContext.xml文件
<bean id="BaseDao" class="com.abgc.fab.dao.BaseDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我的basao类(class):
public class BaseDao<TEntity> extends CommonDao<TEntity> implements IBaseDao<TEntity> {
}
public abstract class CommonDao<TEntity> extends FabObject implements ICommonDao<TEntity> {
public CommonDao() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
this.classEntity = (Class<TEntity>) pt.getActualTypeArguments()[0];
}
请问有什么帮助吗?
最佳答案
TEntity 是实际的类还是您的类型参数?
在我看来,TEntity 只是类型参数的名称,我认为这不起作用。
你需要类似的东西
class ConcreteNominationHibernateDAO
extends BaseDao<ConcreteNominationSubclass> {...}
关于java - Spring依赖注入(inject)通用服务类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28046048/