本文介绍了测试DAO时出错:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法转换为java.lang.Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Spring + Hibernate非常新,并且正在运行测试以查看我的DAO是否正在运行。我在Google中搜索时得到以下错误会产生很多不同的答案(主要是我检查过的拼写错误)。我希望你能帮我解决这个问题:
java.lang.IllegalStateException:无法加载ApplicationContext
在org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
。在组织.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
在org.springframework .test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
。在组织。 junit.internal.runners。 model.ReflectiveCallable.run(ReflectiveCallable.java:15)
处org.springframework.test.context org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
。 junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
在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)
在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)
在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)
在org.springframework .test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4。 statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
处org.springframework.test.context.junit4.SpringJUnit4ClassRunner org.junit.runners.ParentRunner.run(ParentRunner.java:236)
。运行(SpringJUnit4ClassRunner.java:180)
在org.junit.runner.JUnitCore.run(JUnitCore.java:157)
在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)在sun.reflect.NativeMethodAccessorImpl.invoke在sun.reflect.NativeMethodAccessorImpl.invoke0
(本机方法)
(NativeMethodAccessorImpl.java:39)
。在com.intellij.rt.execution.application。 AppMain.main(AppMain.java:120)
导致:org.springframework.beans.factory.BeanCreationException:创建bean时出错名称'nominationDao'定义在文件[C:\IdeaProjects\Projects\branch\dev\test-dataacess\target\classes\com\dev\test\data\dao\ hibernate \NominationHibernateDAO.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.dev.test.data.dao.hibernate.NominationHibernateDAO]:构造函数抛出异常;嵌套的例外是java.lang.ClassCastException:sun.reflect.generics.reflectiveObjects.TypeVariableImpl不能org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java被转换为java.lang.Class中的
: 288)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java: 456)
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:2 95)
。在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
。在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java: 292)
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900 580)
)
在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.AbstractGen ericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
在org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
在org.springframework.test.context.TestContext.getApplicationContext( TestContext.java:304)
... 27 more
导致:org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.dev.test.data.dao.hibernate.NominationHibernateDAO] :构造函数抛出异常;嵌套的例外是java.lang.ClassCastException:sun.reflect.generics.reflectiveObjects.TypeVariableImpl不能org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)$ B被转换为java.lang.Class中的
在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
$ b在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
... 42 more
导致:java.lang.ClassCastException:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法转换为java.lang.Class
在com.dev.test.data .dao.hibernate.AbstractHibernateDAO<初始化>(AbstractHibernateDAO.java:50)在com.dev.test.data.dao.hibernate.NominationHibernateDAO
将INIT>(NominationHibernateDAO.java:36)$ b在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)$ b在sun.reflect
。 NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
在java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 44 more
这可能是什么原因造成的?如果我在我的AbstractHibernateDAO和NominationHibernateDAO中进行了编辑,请告诉我们。
谢谢!
片段:
这是AbstractHibernateDAO的一个片段,第50行是注释:
public abstract class AbstractHibernateDAO< T,K extends Serializable>
实现了DAO< T,K> {
受保护的Logger logger = LoggerFactory.getLogger(this.getClass());
private SessionFactory sessionFactory;
私人课程< T>对PersistentClass;
protected AbstractHibernateDAO(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
this.persistentClass =(Class< T>)((ParameterizedType)// LINE 50
getClass()。getGenericSuperclass())。getActualTypeArguments()[0];
$ / code>
第36行后面是关于NominationHibernateDAO的这段代码的评论:
@Repository(nominationDao)
public class NominationHibernateDAO< T extends Nomination>
extends AbstractHibernateDAO< T,Integer>
实现了NominationDAO< T> {
@Autowired(required = true)
public NominationHibernateDAO(SessionFactory sessionFactory){
super(sessionFactory); // LINE 36
code $ <$ $ $ $ $ $ $ $ <$ class =h2_lin>解决方案
表达式
getClass()。getGenericSuperclass())。getActualTypeArguments()[0]
指代代替 AbstractHibernateDAO 的第一个参数的值(它在 AbstractHibernateDAO.java 由 T )表示。被子类 NominationHibernateDAO 取代的类型仍然不具体,它是 T extends Nomination ,这绝对不是类型实例,但是类型实例( TypeVariableImpl 是一个类型的实现。)如果 NominationHibernateDAO 被声明为类似的东西:
class NominationHibernateDAO extends AbstractHibernateDAO< Nomination,Integer> {...
然后 AbstractHibernateDAO 构造函数魔法会奏效。或者,您可以(或者甚至应该)实例化一个如下所示的 NominationHibernateDAO 的子类:
class ConcreteNominationHibernateDAO
extends NominationHibernateDAO< ConcreteNominationSubclass> {...
或
<$ p $新的NominationHibernateDAO< ConcreteNominationSubclass>(){}
不会触发问题。
您确定 NominationHibernateDAO 类是用作Spring bean吗?
I am very new to Spring + Hibernate, and I am running tests to see if my DAOs are working. I get the following error which upon searching in Google yields a lot of different answers (mostly of typos, which I have checked). 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.
Thank you!
Edit here are the snippets:
This is a snippet for AbstractHibernateDAO, line 50 is by a comment:
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:
@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). 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).
If the NominationHibernateDAO was declared with something like that:
class NominationHibernateDAO extends AbstractHibernateDAO<Nomination, Integer> { ...
then the AbstractHibernateDAO constructor magic would work. Alternatively, you can (or you even should?) instantiate a subclass of NominationHibernateDAO declared like this:
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?
这篇关于测试DAO时出错:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法转换为java.lang.Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!