小编典典

创建名称为'sessionFactory'的bean时出错:MalformedParameterizedTypeException

hibernate

任何建议将不胜感激。我难过…

问题

在我的笔记本电脑上,只要我们的应用程序尝试加载application-
config.xml,我都会收到java.lang.reflect.MalformedParameterizedTypeException。

完全相同的代码可在我的台式机和同事的台式机/笔记本电脑上使用。但是在我的笔记本电脑上,它将引发此错误。由于我的笔记本电脑和台式机是完全相同的开发环境(Mac
OS X Leopard上的Eclipse中的Java 1.6 Maven项目),我通过执行以下操作缩小了原因:

  • 从Subversion签出了一个新项目(因此没有代码差异)
  • 复制并替换了我的整个Eclipse目录(因此没有Eclipse版本/插件差异)
  • 复制并替换了我的整个.m2目录(因此没有maven / project依赖项差异)

在一个地方而不是另一个地方可能导致此错误的原因是什么?以下是相关bean和错误的摘要…


片段:application-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

...
    <bean id="dataServiceRepository" class="com.saic.gnosis.dao.DataServiceDao">
    <constructor-arg ref="sessionFactory" />
    </bean>
...
    <bean id="implementationRepository" class="com.saic.gnosis.dao.ImplementationDao">
    <property name="dataServiceRepository" ref="dataServiceRepository" />
    <property name="implementationDetailRepository" ref="implementationDetailRepository" />
    <property name="implementationStrategyFactory" ref="implementationStrategyFactory" />
    </bean>
...
    <bean id="implementationStrategyFactory" class="com.saic.gnosis.plant.ImplementationStrategyPlant" />
...
    <bean id="implementationDetailRepository" class="com.saic.gnosis.dao.ImplementationDetailDao">
    <constructor-arg ref="sessionFactory" />
    </bean>
...
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="mysqlDataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.saic.gnosis.model</value>
            <value>com.saic.gnosis.model.observable</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.c3p0.min_size">2</prop> <!-- Minimum number of Connections a pool will maintain at any given time. -->
            <prop key="hibernate.c3p0.max_size">9</prop> <!-- Maximum number of Connections a pool will maintain at any given time. -->
            <prop key="hibernate.c3p0.timeout">10</prop> <!-- Seconds a Connection can remain pooled but unused before being discarded. 
                Zero means idle connections never expire. -->
            <prop key="hibernate.c3p0.max_statements">0</prop> <!-- The size of c3p0's global PreparedStatement cache. If both maxStatements 
                and maxStatementsPerConnection are zero, statement caching will not be enabled. 
                If maxStatements is zero but maxStatementsPerConnection is a non-zero value, 
                statement caching will be enabled, but no global limit will be enforced, 
                only the per-connection maximum. maxStatements controls the total number 
                of Statements cached, for all Connections. If set, it should be a fairly 
                large number, as each pooled Connection requires its own, distinct flock 
                of cached statements. As a guide, consider how many distinct PreparedStatements 
                are used frequently in your application, and multiply that number by maxPoolSize 
                to arrive at an appropriate value. Though maxStatements is the JDBC standard 
                parameter for controlling statement caching, users may find c3p0's alternative 
                maxStatementsPerConnection more intuitive to use. -->
        </props>

        </property>
    </bean>

    <bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:mysql://database.com/ourProject" />
            <property name="user" value="ourUser" />
            <property name="password" value="ourPass" />
            <property name="initialPoolSize" value="4" />
    </bean>

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

片段:JUnit错误跟踪:

java.lang.ExceptionInInitializerError
        at com.saic.gnosis.updater.task.ConnectionCommandFileWriterTask.<init>(ConnectionCommandFileWriterTask.java:16)
        at com.saic.gnosis.updater.CiscoDeviceConfiguratorTest.initializeSpyObjects(CiscoDeviceConfiguratorTest.java:120)
        at com.saic.gnosis.updater.CiscoDeviceConfiguratorTest.initializeTestObjects(CiscoDeviceConfiguratorTest.java:110)
        at com.saic.gnosis.updater.CiscoDeviceConfiguratorTest.setUp(CiscoDeviceConfiguratorTest.java:105)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'implementationRepository' defined in class path resource [beans/application-config.xml]: Cannot resolve reference to bean 'dataServiceRepository' while setting bean property 'dataServiceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataServiceRepository' defined in class path resource [beans/application-config.xml]: Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans/application-config.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.saic.gnosis.dao.BeanDao.<clinit>(BeanDao.java:35)
        ... 28 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataServiceRepository' defined in class path resource [beans/application-config.xml]: Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans/application-config.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:495)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:162)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 46 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans/application-config.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 61 more
Caused by: java.lang.reflect.MalformedParameterizedTypeException
        at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(ParameterizedTypeImpl.java:42)
        at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(ParameterizedTypeImpl.java:35)
        at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(ParameterizedTypeImpl.java:77)
        at sun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(CoreReflectionFactory.java:86)
        at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:122)
        at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:31)
        at sun.reflect.generics.repository.ClassRepository.getSuperInterfaces(ClassRepository.java:82)
        at java.lang.Class.getGenericInterfaces(Class.java:794)
        at org.springframework.core.GenericTypeResolver.getTypeVariableMap(GenericTypeResolver.java:161)
        at org.springframework.core.GenericTypeResolver.resolveReturnType(GenericTypeResolver.java:99)
        at org.springframework.beans.GenericTypeAwarePropertyDescriptor.getPropertyType(GenericTypeAwarePropertyDescriptor.java:88)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
        at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
        ... 70 more

编辑:

更新-我只是找到了另一个线索,我们所有与数据库相关的JUnit测试都不起作用。在我的桌面上,它们全部通过。在我的笔记本电脑上,它们全部失败,但出现以下异常:

java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.<init>(Ljava/lang/Class;Ljava/util/Collection;)V

任何建议都欢迎!谢谢!


阅读 257

收藏
2020-06-20

共1个答案

小编典典

我认为您的弹簧瓶有误-也许一个用于弹簧2.5,另一个用于3.0。例如,类加载器加载3.0版的spring-core,其余的加载2.5版。这将导致此类错误。

我建议仔细检查您的Maven依赖关系图并删除所有多余的依赖关系图。如有必要,请<exclusions>在pom中使用。

在此之前,请在所有计算机上清理项目。

2020-06-20