问题描述
Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'sqlSessionFactory'definedinclasspathresource[spring/applicationContext.xml]:Initializationofbeanfailed;nestedexceptionisjava.lang.reflect.MalformedParameterizedTypeExceptionatorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)atjava.security.AccessController.doPrivileged(NativeMethod)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:308)atorg.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:947)atorg.springframework.context.support.AbstractApplicationContext.registerListeners(AbstractApplicationContext.java:701)atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:377)atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)atcom.tgood.tc.cp.businessbase.test.main(test.java:16)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:597)atcom.intellij.rt.execution.application.AppMain.main(AppMain.java:134)Causedby:java.lang.reflect.MalformedParameterizedTypeExceptionatsun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(ParameterizedTypeImpl.java:42)atsun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(ParameterizedTypeImpl.java:35)atsun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(ParameterizedTypeImpl.java:77)atsun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(CoreReflectionFactory.java:86)atsun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:122)atsun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:31)atsun.reflect.generics.repository.ClassRepository.getSuperInterfaces(ClassRepository.java:82)atjava.lang.Class.getGenericInterfaces(Class.java:796)atorg.springframework.core.GenericTypeResolver.getTypeVariableMap(GenericTypeResolver.java:154)atorg.springframework.core.GenericTypeResolver.resolveParameterType(GenericTypeResolver.java:83)atorg.springframework.beans.GenericTypeAwarePropertyDescriptor.getWriteMethodParameter(GenericTypeAwarePropertyDescriptor.java:109)atorg.springframework.beans.GenericTypeAwarePropertyDescriptor.getPropertyType(GenericTypeAwarePropertyDescriptor.java:91)atorg.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)atorg.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)...20moreapplicationContext.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--采用注释的方式配置bean--><context:annotation-config/><!--配置要扫描的包--><context:component-scanbase-package="com.tgood.tc.cp.businessbase"></context:component-scan><!--加载jdbc.properties配置文件--><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"><list><value>classpath:spring/jdbc.properties</value></list></property></bean><!--配置数据源(连接池,proxool)--><beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><propertyname="driverClass"><value>${jdbc.driverClassName}</value></property><propertyname="jdbcUrl"><value>${jdbc.url}</value></property><propertyname="user"><value>${jdbc.username}</value></property><propertyname="password"><value>${jdbc.password}</value></property><!--连接池中保留的最小连接数。--><propertyname="minPoolSize"><value>6</value></property><!--连接池中保留的最大连接数。Default:15--><propertyname="maxPoolSize"><value>50</value></property><!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default:3--><propertyname="initialPoolSize"><value>10</value></property><!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default:0--><propertyname="maxIdleTime"><value>60</value></property><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3--><propertyname="acquireIncrement"><value>3</value></property><!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0--><propertyname="maxStatements"><value>0</value></property><!--连接池用完时客户调用getConnection()后等待获取连接的时间,单位:毫秒。超时后会抛出--><!--SQLEXCEPTION,如果设置0,则无限等待。Default:0--><propertyname="checkoutTimeout"value="3000"/><!--每60秒检查所有连接池中的空闲连接。Default:0--><propertyname="idleConnectionTestPeriod"><value>60</value></property><!--定义在从数据库获取新连接失败后重复尝试的次数。Default:30--><propertyname="acquireRetryAttempts"><value>30</value></property><!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default:false--><propertyname="breakAfterAcquireFailure"><value>false</value></property><!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。Default:false--><propertyname="testConnectionOnCheckout"><value>false</value></property></bean><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!--Session工厂--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="configLocation"value="classpath:spring/mybatis.xml"/><propertyname="dataSource"ref="dataSource"/></bean><!--配置SqlSessionTemplate--><beanid="userDao"class="org.mybatis.spring.mapper.MapperFactoryBean"><propertyname="mapperInterface"value="com.tgood.tc.cp.businessbase.dao.UserMapper"/><propertyname="sqlSessionFactory"ref="sqlSessionFactory"/></bean></beans>
jdbc.propertiesjdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://114.215.159.133:3306/chargenet?useUnicode=true&characterEncoding=utf8;zeroDateTimeBehavior=convertToNulljdbc.username=TgoodMy321jdbc.password=passTmy246mybatis.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><typeAliases><typeAliasalias="user"type="com.tgood.tc.cp.businessbase.model.User"/></typeAliases><!--指定映射文件--><mappers><mapperresource="dao/UserMapper.xml"/></mappers></configuration>
解决方案
解决方案二:
<mapperresource="dao/UserMapper.xml"/>这个应该写全路径<mapperresource="com/tgood/tc/cp/businessbase/dao/UserMapper.xml"/>
解决方案三:
引用1楼nd707355117的回复:
<mapperresource="dao/UserMapper.xml"/>这个应该写全路径<mapperresource="com/tgood/tc/cp/businessbase/dao/UserMapper.xml"/>
谢谢你的回答,不过我该成你说的这样还是报同样的错误。
解决方案四:
这是用到的jar包<properties><junit.version>4.11</junit.version><commons-logging.version>1.1.1</commons-logging.version><dubbo.version>2.5.3</dubbo.version><javassist.version>3.15.0-GA</javassist.version><spring.version>3.2.6.RELEASE</spring.version><slf4j.version>1.6.6</slf4j.version><log4j.version>1.2.9</log4j.version><zookeeper.version>3.4.5</zookeeper.version><zkclient.version>0.4</zkclient.version><mybatis.version>3.2.3</mybatis.version><mybatis-spring.version>1.1.1</mybatis-spring.version></properties><dependencies><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>${commons-logging.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>${javassist.version}</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>${zookeeper.version}</version><type>pom</type></dependency><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>${zkclient.version}</version></dependency><!--spring核心包--><!--springframestart--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-oxm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><!--springframeend--><!--junit测试包--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!--日志文件管理包--><!--logstart--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!--logend--><dependency><groupId>com.tgood.tc.cp.icloud</groupId><artifactId>iCloud-BusinessBase-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.2.1</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.2</version></dependency></dependencies>
解决方案五:
jdbc.url=jdbc:mysql://114.215.159.133:3306/chargenet?useUnicode=true&characterEncoding=utf8;zeroDateTimeBehavior=convertToNull没有用过=这种写法,都是直接用=
解决方案六:
引用4楼Inhibitory的回复:
jdbc.url=jdbc:mysql://114.215.159.133:3306/chargenet?useUnicode=true&characterEncoding=utf8;zeroDateTimeBehavior=convertToNull没有用过=这种写法,都是直接用=
谢谢你的回答,不过把你说的去掉之后还是报同样的错误。
解决方案七:
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="configLocation"value="classpath:spring/mybatis.xml"/><propertyname="dataSource"ref="dataSource"/></bean>
检查看mybatis.xml的配置
解决方案八:
引用6楼Inhibitory的回复:
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="configLocation"value="classpath:spring/mybatis.xml"/><propertyname="dataSource"ref="dataSource"/></bean>检查看mybatis.xml的配置
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><typeAliases><typeAliasalias="user"type="com.tgood.tc.cp.businessbase.model.User"/></typeAliases><!--指定映射文件--><mappers><mapperresource="com/tgood/tc/cp/businessbase/dao/UserMapper.xml"/></mappers></configuration>
我的配置是这样
解决方案九:
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><typeAliases><typeAliasalias="user"type="com.tgood.tc.cp.businessbase.model.User"/></typeAliases><!--指定映射文件--><mappers><mapperresource="dao/UserMapper.xml"/></mappers></configuration>
这样也不行
解决方案十:
我猜要么是id重名,要么就是bean重复加载。用删bean慢慢排除
解决方案十一:
引用9楼sphinx1685的回复:
我猜要么是id重名,要么就是bean重复加载。用删bean慢慢排除
我说的是mybatis中的sqlid,不是spring的beanid
解决方案十二:
UserMapper.xml我一般是如下来加载的,不用configLocation<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="mapperLocations"value="classpath*:sample/config/mappers/**/*.xml"/></bean>
可以试试这种方法。
解决方案十三:
Spring与MyBatis集成的官网文档
解决方案十四:
Errorcreatingbeanwithname'sqlSessionFactory'definedinclasspathresource[spring/applicationContext.xml]:这句话是关键
解决方案十五:
还是目录结构的问题,分不清你哪个是source文件夹,哪个是普通包<mapperresource="main/resources/dao/UserMapper.xml"/><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:main/resources/spring/mybatis.xml" /> <property name="dataSource" ref="dataSource" /> </bean>
解决方案:
试试吧,看报其他错没
解决方案:
引用14楼nd707355117的回复:
还是目录结构的问题,分不清你哪个是source文件夹,哪个是普通包<mapperresource="main/resources/dao/UserMapper.xml"/><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:main/resources/spring/mybatis.xml" /> <property name="dataSource" ref="dataSource" /> </bean>
我这个是maven结构,不存在main和resources文件夹的。按照你的写法直接爆红了。就是这么写:spring/mybatis.xml可以直接点击到
解决方案:
引用13楼j762878367的回复:
Errorcreatingbeanwithname'sqlSessionFactory'definedinclasspathresource[spring/applicationContext.xml]:这句话是关键
我也知道这是关键,关键是怎么处理。
解决方案:
引用11楼Inhibitory的回复:
UserMapper.xml我一般是如下来加载的,不用configLocation<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="mapperLocations"value="classpath*:sample/config/mappers/**/*.xml"/></bean>可以试试这种方法。
这样写也报同样的错误。
解决方案:
dubbo和你的spring包冲突吧,dubbo本身自带spring的参考下这个帖子试试:http://blog.csdn.net/xiazou/article/details/20545555