我使用泛型创建了接口和类的层次结构,并弄乱了所有内容。
最顶级的类是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)
public abstract class GenericDaoImpl<T extends AbstractJpaEntity> implements GenericDao<T> {...
接口ProductTypeDao
public interface ProductTypeDao<T extends ProductTypeDomain> extends GenericDao<T> {
Spring存储库类ProductTypeDaoImpl
@Repository("productTypeDao")
public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao
{....
在spring服务类ProductManagerServiceimpl中,我正在自动装配存储库productTypeDao
@Service("productManager")
public class ProductManagerServiceimpl implements 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’的bean时出错dao / impl /
ProductTypeDaoImpl.class]:实例化bean失败;
无法实例化bean类[com.jodo.cms.dao.impl.ProductTypeDaoImpl]:构造方法引发了异常;
嵌套异常是java.lang.ClassCastException:无法将java.lang.Class强制转换为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.html" target="_blank">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
类GenericDaoImpl的详细信息
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);
}
}...
我不能更改AbstractJpaEntity,GenericDao和GenericDaoImpl,因为它已经用于合并许多其他未扩展ProductTypeDomain的实体。
谢谢
在这个类的声明中
public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao
您正在使用GenericDaoImpl
和ProductTypeDao
作为原始类型。首先阅读为什么不应该使用它们。
您正在使用原始类型的事实在这里引起问题
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
自以来,在哪里getGenericSuperclass()
将返回Class
实例GenericDaoImpl
public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao
未参数化,因此不是ParameterizedType
。然后强制转换失败。
解决方案是在类声明中参数化这两种类型。从您的代码中并不能立即看出这些类型参数应该是什么,但GenericTypeDao
应该采用ProductTypeDomain
public class ProductTypeDaoImpl extends GenericDaoImpl<ProductTypeDomain> implements ProductTypeDao
并且您的接口ProductTypeDao
可能应该声明为
public interface ProductTypeDao extends GenericDao<ProductTypeDomain> {
问题内容: 我是Java Reflection的新手。 我检查了很多论坛,但找不到有效的解决方案。 错误: 当我试图从数据库获取对象时: 方法声明 二级扩展 实体类: 并在控制器中调用方法: 这很奇怪,因为我有其他实体可以在没有任何运行时错误的情况下使用该方法。这是该实体: 并在其他控制器中调用该方法: 此代码有效。我看不到模型和控制器之间的任何区别。 有人可以帮我指出我的错误吗? -------
问题内容: 我长期困扰这个问题。我有一段时间搜索此问题,但没有解决方案。 结构体: 我也用我 在我的。 请提供有关如何解决此问题的一些信息。 问题答案: 从中删除注释并使其: 发生您的问题是因为的特化,这意味着Spring将尝试创建注入实例。由于superclass()不是通用的,因此您无法将其向下转换为,因此这行代码将失败(与尝试使用手动实例化该代码的方式相同): 专业课仍应使用注释。当spri
问题内容: 我有MainActivity类,该类具有实例化ApplicationBar的方法,所有其他Activity都从此继承,因此他们可以使用此方法。 但是我也有一个MapHolder类,它必须从xml中扩展,因为它使用来显示地图。问题是如果我从不显示ApplicationBar选项卡扩展它,并且从MainActivity扩展它而不显示地图,则出现此错误: 问题答案: 您正在尝试将a强制转换为
问题内容: 我遇到以下错误: 用下面的代码 我为什么不能这样做? 问题答案: 问题来自杰克逊。如果没有足够的信息反序列化到哪个类,则使用。 既然你不通知你的元素类型的杰克逊,它不知道你要反序列化到的秒。因此,它恢复为默认值。 相反,您可能可以使用,然后以比保证价格允许的方式丰富的方式进行处理。像这样:
问题内容: 我最近升级了一个应用程序,使其可以使用JAVA 7和JBoss 7.1.1运行。该应用程序最初是在JAVA 5和Jboss 4.2.2上开发的。该应用程序使用hibernate3进行持久化。 在新平台上,尝试插入具有上述错误的带有CLOB字段的表时,应用程序失败。我正在使用ojdbc14.jar(后端数据库Oracle 10.2.0.3) 这些是我在jboss 7.1.1配置中验证的:
问题内容: 我正在运行以下查询。它显示一条错误消息。如何解决这个错误? 错误是: 问题答案: 您可以这样写: 已经是一个字符串列表,因为您只选择了一列。 此外,正如Thilo的答案所暗示的那样,您可以将结果直接转换为a 而不是使用。