简体   繁体   English

测试DAO时出错:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法强制转换为java.lang.Class

[英]Error testing DAOs: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class

I am very new to Spring + Hibernate, and I am running tests to see if my DAOs are working. 我是Spring + Hibernate的新手,我正在运行测试以查看我的DAO是否正常工作。 I get the following error which upon searching in Google yields a lot of different answers (mostly of typos, which I have checked). 我收到以下错误,在Google上搜索会产生很多不同的答案(主要是拼写错误,我已经检查过)。 I'm hoping you can help me out on this: 我希望你能帮我解决这个问题:

java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nominationDao' defined in file [C:\IdeaProjects\Projects\branch\dev\test-dataacess\target\classes\com\dev\test\data\dao\hibernate\NominationHibernateDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.dev.test.data.dao.hibernate.NominationHibernateDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
        at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
        ... 27 more
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.dev.test.data.dao.hibernate.NominationHibernateDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
        ... 42 more
    Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
        at com.dev.test.data.dao.hibernate.AbstractHibernateDAO.<init>(AbstractHibernateDAO.java:50)
        at com.dev.test.data.dao.hibernate.NominationHibernateDAO.<init>(NominationHibernateDAO.java:36)
        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:147)
        ... 44 more

What could cause this? 什么可能导致这个? Please let me know if I have edit in my AbstractHibernateDAO and NominationHibernateDAO. 如果我在AbstractHibernateDAO和NominationHibernateDAO中编辑,请告诉我。

Thank you! 谢谢!

Edit here are the snippets: 这里编辑的是片段:

This is a snippet for AbstractHibernateDAO, line 50 is by a comment: 这是AbstractHibernateDAO的片段,第50行是注释:

public abstract class AbstractHibernateDAO<T, K extends Serializable>
        implements DAO<T, K> {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    private SessionFactory sessionFactory;

    private Class<T> persistentClass;

    protected AbstractHibernateDAO(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.persistentClass = (Class<T>) ((ParameterizedType) //LINE 50
                getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

Line 36 is followed by a comment on this snippet for NominationHibernateDAO: 第36行之后是对NominationHibernateDAO的这个片段的评论:

@Repository("nominationDao")
public class NominationHibernateDAO<T extends Nomination>
        extends AbstractHibernateDAO<T,Integer>
        implements NominationDAO<T> {


      @Autowired(required = true)
      public NominationHibernateDAO(SessionFactory sessionFactory) {
           super(sessionFactory); //LINE 36
      }

The expression 表达方式

getClass().getGenericSuperclass()).getActualTypeArguments()[0]

refers to value substituted to first type parameter of AbstractHibernateDAO (which in the AbstractHibernateDAO.java is denoted by T ). 指的是替换为AbstractHibernateDAO第一个类型参数的值(在AbstractHibernateDAO.java中用T表示)。 The type substituted by subclass NominationHibernateDAO is still not concrete, it is T extends Nomination , which is definitely not a java.lang.Class instance, but a Type instance ( TypeVariableImpl is an implementation of a Type ). 子类NominationHibernateDAO替换的类型仍然不具体,它是T extends Nomination ,它绝对不是java.lang.Class实例,而是Type实例( TypeVariableImplType的实现)。

If the NominationHibernateDAO was declared with something like that: 如果使用类似的东西声明NominationHibernateDAO

class NominationHibernateDAO extends AbstractHibernateDAO<Nomination, Integer> { ...

then the AbstractHibernateDAO constructor magic would work. 然后AbstractHibernateDAO构造函数魔术会起作用。 Alternatively, you can (or you even should?) instantiate a subclass of NominationHibernateDAO declared like this: 或者,您可以(或者您应该?)实例化NominationHibernateDAO的子类,如下所示:

class ConcreteNominationHibernateDAO
        extends NominationHibernateDAO<ConcreteNominationSubclass> { ...

or 要么

new NominationHibernateDAO<ConcreteNominationSubclass>() {}

This, again, would not trigger the problem. 这再一次也不会引发问题。

Are you sure the NominationHibernateDAO class is meant to be used as a Spring bean? 您确定NominationHibernateDAO类是否可用作Spring bean?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设计模式:Lazy Singleton,Generics和Inheritance:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - Design Pattern: Lazy Singleton, Generics and Inheritance: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.ClassCastException:无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType - java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized - class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized ClassCastException:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - ClassCastException:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 错误:(56,0)原因:org.gradle.api.tasks.testing.Test_Decorated无法转换为java.lang.Class - Error:(56, 0) Cause: org.gradle.api.tasks.testing.Test_Decorated cannot be cast to java.lang.Class java.lang.Class和泛型 - java.lang.Class and generics sun.reflect.generics.reflectiveObjects.NotImplementedException和org.apache.commons.lang3.NotImplementedException之间有什么区别 - What is the difference between sun.reflect.generics.reflectiveObjects.NotImplementedException and org.apache.commons.lang3.NotImplementedException 引起:java.lang.ClassCastException:使用Generic Type时,libcore.reflect.ParameterizedTypeImpl无法强制转换为java.lang.Class - Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type “无法转换为java.lang.Class”,即:哪种类型不是类 - “Cannot be cast to java.lang.Class”, namely: which type is not a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM