gpt4 book ai didi

java.lang.Class 不能强制转换为 java.lang.reflect.ParameterizedType

转载 作者:IT老高 更新时间:2023-10-28 13:59:22 25 4
gpt4 key购买 nike

我被这个问题困扰了很长时间。我搜索了这个问题一段时间,但没有一个解决方案有效。

结构:

public interface GenericDAO<T extends Serializable, ID extends Serializable>

@Repository
public class AbstractGenericDAO<T extends Serializable, ID extends Serializable>
implements GenericDAO<T, ID> {

private Class<T> persistentClass;

@Autowired
private SessionFactory sessionFactory;

static Logger LOGGER = Logger.getLogger(AbstractGenericDAO.class);


@SuppressWarnings("unchecked")
public AbstractGenericDAO() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

/**
* @param entity
* @return T
* @throws DBException
*/
@SuppressWarnings("unchecked")
public T saveEntity(T entity) throws DBException {
return saveEntity(entity, false);
}

/**
* @param entity
* @param explicitFlush
* @return T
* @throws DBException
*/
@SuppressWarnings("unchecked")
public T saveEntity(T entity, boolean explicitFlush) throws DBException {
Session session = getSessionFactory().getCurrentSession();

try {
session.save(entity);
if(explicitFlush) {
session.flush();
session.refresh(entity);
}
} catch (HibernateException he) {
String errorMsg = "Could not save entity. Reason: " + he.getMessage();
LOGGER.error(errorMsg, he);
throw new DBException(errorMsg, he);
}

return entity;
}

/* (non-Javadoc)
* @see com.amazon.fc.receive.dbaccess.dao.GenericDAO#getPersistentClass()
*/
@SuppressWarnings("unchecked")
public Class<T> getPersistentClass() {
return persistentClass;
}

/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}

/**
* @param sessionFactory the sessionFactory to set
*/
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}

public interface ShlkActiveWorkflowDAO
extends GenericDAO<ShlkActiveWorkflow, Serializable>

@Repository
public class ShlkActiveWorkflowDAOImpl
extends AbstractGenericDAO<ShlkActiveWorkflow, Serializable>
implements ShlkActiveWorkflowDAO

我也在使用 <context:component-scan>在我的application-config.xml + <tx:annotation-driven />在我的application-config.xml .

请提供一些有关如何解决此问题的信息。

Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'abstractGenericDAO'

Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:890)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
at com.coral.spring.Launcher.<init>(Launcher.java:95)
at com.coral.spring.Launcher.main(Launcher.java:56)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.workflow.dao.AbstractGenericDAO]: Constructor threw exception; nested exception is
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:938)
... 12 more
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.workflow.dao.AbstractGenericDAO.<init>(AbstractGenericDAO.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
... 14 more

最佳答案

删除 @Repository来自 AbstractGenericDAO 的注释并使它成为 abstract :

public abstract class AbstractGenericDAO<T extends Serializable, ID extends Serializable> 
implements GenericDAO<T, ID>

出现您的问题是因为 @Repository@Component 的特化,这意味着 Spring 将尝试创建 AbstractGenericDAO注入(inject)实例。从 AbstractGenericDAO父类(super class) ( Object ) 不是通用的,您将无法向下转换它的 TypeParameterizedType ,因此这行代码将失败(与尝试使用 new AbstractGenericDAO() 手动实例化它的方式相同):

this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

专业类ShlkActiveWorkflowDAOImpl仍应使用 @Repository 进行注释.当 spring 尝试创建此类的实例时,隐式调用 AbstractGenericDAO构造函数将发生,但这次上面提到的代码行将按预期运行。发生这种情况是因为 getClass()返回 ShlkActiveWorkflowDAOImpl.class它是泛型 AbstractGenericDAO 的子类(所以对 ParameterizedType 的失望是有效的)。

自从 ShlkActiveWorkflowDAOImpl extends AbstractGenericDAO<ShlkActiveWorkflow, Serializable>实际类型ShlkActiveWorkflow将在运行时正确反射(reflect)。这是避免传递 Class<T> 的已知解决方法。引用 AbstractGenericDAO构造函数。

如果您担心 @Autowired注释在 AbstractGenericDAO ,不要。当您注入(inject)其子类之一的实例时,Spring 将正确连接所有内容。

关于java.lang.Class 不能强制转换为 java.lang.reflect.ParameterizedType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11067512/

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