java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType [英] java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

查看:319
本文介绍了java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用泛型创建了接口和类的层次结构,搞砸了一切。


最高级的类是由所有域实体扩展的AbstractJpaEntity

  @MappedSuperclass 
@EntityListeners({AbstractJpaEntity.AbstractEntityListener.class})
@SuppressWarnings(serial)
public class AbstractJpaEntity implements Serializable

ProductTypeDomain类就像标记类一样分离出很少的表实体。

  @ SuppressWarnings(serial)
@MappedSuperclass
@EntityListeners({ProductTypeDomain.AbstractEntityListener.class})
public class ProductTypeDomain extends AbstractJpaEntity {}

接口GenericDao定义

  public interface GenericDao< T> {... 

抽象类GenericDaoImpl(该类具有通用功能,如persist,merge) p>

  public abstract class GenericDaoImpl< T extends AbstractJpaEntity>实现GenericDao< T> {... 

接口ProductTypeDao

  public interface ProductTypeDao< T extends ProductTypeDomain>扩展GenericDao< T> {

Spring存储库类ProductTypeDaoImpl

  @Repository(productTypeDao)
public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao
{...

在Spring服务类ProductManagerServiceimpl中,我是自动装配存储库productTypeDao

  @ Service(productManager)
public class ProductManagerServiceimpl实现ProductManagerService {
@Autowired
ProductTypeDao productTypeDao;

在运行代码时,它给了我以下错误:

引起:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.service.ProductManagerService com.jodo.cms.controllers.ProductController.productManager;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'productManager'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[/ home / jodo / Server / Tomcat / webapps / jodocms / WEB-INF / classes / com / jodo / cms /中定义名称'productTypeDao' dao / impl / ProductTypeDaoImpl.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType

  Full Stacktrace是
-------------------------------
09:24:29,443 INFO XmlWebApplicationContext:1020 - 关闭根WebApplicationContext:启动日期[Tue Apr 15 08:38:10 IST 2014];上下文层次结构的根
09:24:29,445 INFO DefaultListableBeanFactory:433 - 销毁org.springframework.beans.factory.support.DefaultListableBeanFactory@153a859b中的单例:定义bean [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org。 springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,productTypeDao,productEntityDao,productCategoryDao,categoryTreeDao,categoryEntityDao,categoryManager, productManager,数据源,的entityManagerFactory,transactionManager的,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config。在ternalTransactionAdvisor,log4jInitialization,org.springframework.context.annotation.ConfigurationClassPostProcessor $ ImportAwareBeanPostProcessor#0];工厂层次结构的根
09:24:29,460 INFO LocalContainerEntityManagerFactoryBean:441 - 关闭持久化单元'default'的JPA EntityManagerFactory
09:24:29,461 INFO SessionFactoryImpl:927 - 关闭
09:24: 41,129 INFO DefaultListableBeanFactory:433 - 破坏org.springframework.beans.factory.support.DefaultListableBeanFactory@c27de92中的单例:定义bean [productController,categoryController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,组织。 springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.vali dation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc。 method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler。 BeanNameUrlHandlerMapping时,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.context.annotation.ConfigurationClassPostProcessor $ ImportAwareBeanPostProcessor#0]; parent:org.springframework.beans.factory.support.DefaultListableBeanFactory@5803bbcc
09:24:41,134错误DispatcherServlet:457 - 上下文初始化失败
org.springframework.beans.factory.BeanCreationException:创建bean with名称'productController':注入自动装配的依赖关系失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.service.ProductManagerService com.jodo.cms.controllers.ProductController.productManager;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'productManager'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[/ home / jodo / Server / Tomcat / webapps / jodocms / WEB-INF / classes / com / jodo / cms /中定义名称'productTypeDao' dao / impl / ProductTypeDaoImpl.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类

[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType
在org.springframework.beans.factory.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)

在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:294 )
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225 )
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193 )
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext (FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java :449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1279)
在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1192)
在org.apache。 catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core。 StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
a t org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
org.apache上的org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
.catalina.core.StandardHost.addChild(StandardHost.java:632)
位于org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1073)
位于org.apache.catalina.startup .HostConfig $ DeployWar.run(HostConfig.java:1857)
at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run (FutureTask.java:262)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)$ b $在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615 )
在java.lang.Thread.run(Thread.java:744)
导致:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.service。 ProductManagerService com.jodo.cms。 controllers.ProductController.productManager;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'productManager'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[/ home / jodo / Server / Tomcat / webapps / jodocms / WEB-INF / classes / com / jodo / cms /中定义名称'productTypeDao' dao / impl / ProductTypeDaoImpl.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:在org.springframework.beans.factory.AnowowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java: 284)
... 33 more
导致:org.springframework.beans.factory.BeanCreationException:创建名为'productManager'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[/ home / jodo / Server / Tomcat / webapps / jodocms / WEB-INF / classes / com / jodo / cms /中定义名称'productTypeDao' dao / impl / ProductTypeDaoImpl.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType
在org.springframework.beans.factory.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)

在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:294 )
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225 )
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197 )
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:269)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193 )
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790 )
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ Autow iredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 35 more
引起:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[/ home / jodo / Server / Tomcat / webapps / jodocms / WEB-INF / classes / com / jodo / cms /中定义名称'productTypeDao' dao / impl / ProductTypeDaoImpl.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:在org.springframework.beans.factory.AnowowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java: 284)
... 48 more
导致:org.springframework.beans.factory.BeanCreationException:在文件[/ home / jodo / Server / Tomcat / webapps中定义名称'productTypeDao'创建bean时出错/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)

在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:294 )
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 50 more
导致:org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造函数抛出异常;嵌套异常是java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType
在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 61 more
导致:java.lang.ClassCastException:无法将java.lang.Class转换为java.lang.reflect.ParameterizedType
,位于com.jodo.cms.dao.impl.GenericDaoImpl 。< init>(GenericDaoImpl.java:42)
at com.jodo.cms.dao.impl.ProductTypeDaoImpl。< init>(ProductTypeDaoImpl.java:19)
at sun.reflect.NativeConstructorAccessorImpl .newInstance0(本地方法)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstru ctorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 63 more

class GenericDaoImpl details

 公共抽象类GenericDaoImpl< T extends AbstractJpaEntity>实现GenericDao< T> {
protected Class< T> entityClass;
Logger logger = Logger.getLogger(GenericDaoImpl.class);
@PersistenceContext
受保护的EntityManager em;
public EntityManager getEntityManager(){
return this.em;
}
@SuppressWarnings(unchecked)
public GenericDaoImpl(){
ParameterizedType genericSuperclass =(ParameterizedType)getClass()。getGenericSuperclass();
this.entityClass =(Class< T>)genericSuperclass.getActualTypeArguments()[0];

$ b $ public void persist(T entity)throws JodoException {
try {
em.persist(entity);
em.flush();
} catch(ConstraintViolationException cve){
抛出新的JodoException(cve.getCause(),违反约束,cve);
}
} ...



I无法更改AbstractJpaEntity,GenericDao和GenericDaoImpl,因为它已用于合并许多其他未扩展ProductTypeDomain的实体。



谢谢

解决方案

类声明

  public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao 

作为原始类型使用 GenericDaoImpl ProductTypeDao 首先阅读为什么你不应该使用它们。



您使用原始类型的事实在这里引起问题

  ParameterizedType genericSuperclass =(ParameterizedType)getClass()。getGenericSuperclass(); 

其中 getGenericSuperclass()将返回一个 Class 实例,因为 GenericDaoImpl ,in

  public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao 

没有参数化,因此不是 ParameterizedType 。然后失败。



解决方案是在您的类声明中对这两种类型进行参数化。从代码中可以很明显地看出这些类型参数应该是什么,但是 GenericTypeDao 应该可能带有 ProductTypeDomain

  public class ProductTypeDaoImpl extends GenericDaoImpl< ProductTypeDomain>执行ProductTypeDao 

和您的界面 ProductTypeDao 被声明为

  public interface ProductTypeDao extends GenericDao< ProductTypeDomain> {


I have created hierarchy of interface and classes using generic and messed up everything.
Topmost class is AbstractJpaEntity which is extended by all domain entity

@MappedSuperclass
@EntityListeners({AbstractJpaEntity.AbstractEntityListener.class})
@SuppressWarnings("serial")
public class AbstractJpaEntity implements Serializable

ProductTypeDomain class is like marker class to separate out few table entity.

@SuppressWarnings("serial")
@MappedSuperclass
@EntityListeners({ ProductTypeDomain.AbstractEntityListener.class })
public class ProductTypeDomain extends AbstractJpaEntity{}

Interface "GenericDao" definition

public interface GenericDao<T> {...

Abstract class GenericDaoImpl(this class has general purpose function like persist, merge)

public abstract class GenericDaoImpl<T extends AbstractJpaEntity> implements GenericDao<T> {...

Interface ProductTypeDao

public interface ProductTypeDao<T extends ProductTypeDomain> extends GenericDao<T> {

Spring repository class ProductTypeDaoImpl

@Repository("productTypeDao")
public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao
{....

In spring service class ProductManagerServiceimpl, I am autowiring repository productTypeDao

@Service("productManager")
public class ProductManagerServiceimpl implements ProductManagerService{
    @Autowired
    ProductTypeDao productTypeDao;

On running code, it is giving me following error

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.service.ProductManagerService com.jodo.cms.controllers.ProductController.productManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeDao' defined in file [/home/jodo/Server/Tomcat/webapps/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

Full Stacktrace is
-------------------------------
09:24:29,443  INFO XmlWebApplicationContext:1020 - Closing Root WebApplicationContext: startup date [Tue Apr 15 08:38:10 IST 2014]; root of context hierarchy
09:24:29,445  INFO DefaultListableBeanFactory:433 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@153a859b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,productTypeDao,productEntityDao,productCategoryDao,categoryTreeDao,categoryEntityDao,categoryManager,productManager,dataSource,entityManagerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,log4jInitialization,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
09:24:29,460  INFO LocalContainerEntityManagerFactoryBean:441 - Closing JPA EntityManagerFactory for persistence unit 'default'
09:24:29,461  INFO SessionFactoryImpl:927 - closing
09:24:41,129  INFO DefaultListableBeanFactory:433 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c27de92: defining beans [productController,categoryController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@5803bbcc
09:24:41,134 ERROR DispatcherServlet:457 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.service.ProductManagerService com.jodo.cms.controllers.ProductController.productManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeDao' defined in file [/home/jodo/Server/Tomcat/webapps/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class 

    [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1279)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1192)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1073)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.service.ProductManagerService com.jodo.cms.controllers.ProductController.productManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeDao' defined in file [/home/jodo/Server/Tomcat/webapps/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
        ... 33 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeDao' defined in file [/home/jodo/Server/Tomcat/webapps/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:269)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
        ... 35 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.dao.ProductTypeDao com.jodo.cms.service.impl.ProductManagerServiceimpl.productTypeDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeDao' defined in file [/home/jodo/Server/Tomcat/webapps/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
        ... 48 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeDao' defined in file [/home/jodo/Server/Tomcat/webapps/jodocms/WEB-INF/classes/com/jodo/cms/dao/impl/ProductTypeDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: 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:997)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
        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:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
        ... 50 more
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jodo.cms.dao.impl.ProductTypeDaoImpl]: 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:162)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
        ... 61 more
    Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
        at com.jodo.cms.dao.impl.GenericDaoImpl.<init>(GenericDaoImpl.java:42)
        at com.jodo.cms.dao.impl.ProductTypeDaoImpl.<init>(ProductTypeDaoImpl.java:19)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
        ... 63 more


class GenericDaoImpl details

public abstract class GenericDaoImpl<T extends AbstractJpaEntity> implements GenericDao<T> {
protected Class<T> entityClass;
Logger logger = Logger.getLogger(GenericDaoImpl.class);
@PersistenceContext
protected EntityManager em;
    public EntityManager getEntityManager() {
        return this.em;
    }
    @SuppressWarnings("unchecked")
    public GenericDaoImpl() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
    }

public void persist(T entity) throws JodoException {
    try {
        em.persist(entity);
        em.flush();
    } catch (ConstraintViolationException cve) {
        throw new JodoException(cve.getCause(), "constraints violated", cve);
    }
}...


I cannot change AbstractJpaEntity, GenericDao and GenericDaoImpl, as it is already used for merging many other entities which are not extending ProductTypeDomain.

Thanks

解决方案

In this class declaration

public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao

you're using GenericDaoImpl and ProductTypeDao as raw types. Start by reading why you shouldn't use them.

The fact that you are using raw types causes problems here

ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();

where getGenericSuperclass() will return a Class instance since GenericDaoImpl, in

public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao

is not parameterized and therefore isn't a ParameterizedType. The cast then fails.

The solution is to parameterize the two types in your class declaration. It's not immediately obvious from your code what those type arguments should be, but GenericTypeDao should probably take ProductTypeDomain

public class ProductTypeDaoImpl extends GenericDaoImpl<ProductTypeDomain> implements ProductTypeDao

and your interface ProductTypeDao should probably be declared as

public interface ProductTypeDao extends GenericDao<ProductTypeDomain> {

这篇关于java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.ParameterizedType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆