<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 数据源,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource"></property>
</bean> <!--
<bean id="userDao" class="com.xxx.ssm.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
--> <!-- 通过扫描器的方式自动加载mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.ssm.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

会报出如下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [spring/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.apache.ibatis.session.defaults.DefaultSqlSessionFactory' to required type 'java.lang.String' for property 'sqlSessionFactoryBeanName'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] to required type [java.lang.String] for property 'sqlSessionFactoryBeanName': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:116)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.atguigu.junit.test.UserMapperTest.setUp(UserMapperTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.apache.ibatis.session.defaults.DefaultSqlSessionFactory' to required type 'java.lang.String' for property 'sqlSessionFactoryBeanName'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] to required type [java.lang.String] for property 'sqlSessionFactoryBeanName': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:474)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1461)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
... 34 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] to required type [java.lang.String] for property 'sqlSessionFactoryBeanName': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459)
... 40 more

原因:因为MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。

解决办法:

第一种方式:
修改
property name="sqlSessionFactory" ref="sqlSessionFactory"

property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"
原理:使用sqlSessionFactoryBeanName注入,不会立即初始化sqlSessionFactory, 所以不会引发提前初始化问题。

第二种:直接删掉
注意:在没有配置这一行时,必须配置一个以sqlSessionFactory命名的org.mybatis.spring.SqlSessionFactoryBean。

最新文章

  1. Oracle --&gt; Vertica 数据类型转换规则
  2. ANSI Common Lisp Practice - My Answers - Chatper - 3
  3. Web jquery表格组件 JQGrid 的使用 - 全部代码
  4. nodejs生成多层目录和生成文件的通用方法
  5. POJ 1979 Red and Black(水题,递归)
  6. Ubuntu设置目录的读写权限(Linux命令chmod 777 dirName)
  7. TDD(测试驱动开发)学习一:初识TDD
  8. GTK+2.0学习——第一个GTK程序
  9. 如何实现php字符串翻转?
  10. Spring Cloud Alibaba到底坑不坑?
  11. Java 线程池的原理及实现
  12. Java中CountDownLatch和CyclicBarrier
  13. Salesforce小知识:累计汇总字段类型
  14. Jetty 9的使用
  15. (转)类(class)和结构(struct)的区别是什么?它们对性能有影响吗?.NET BCL里有哪些是类(结构),为什么它们不是结构(类)?在自定义类型时,您如何选择是类还是结构?
  16. JavaScript权威指南1.概述
  17. webpack编译时No PostCSS Config的解决方法
  18. jQuery事件:bind、delegate、on的区别
  19. 【动态规划】【斜率优化】CDOJ1689 分序列
  20. 116th LeetCode Weekly Contest Maximum Width Ramp

热门文章

  1. hdu 4747 线段树/DP
  2. nginx 常用的命令和配置文件
  3. 清北学堂提高组突破营游记day3
  4. 集合(四) Hashtable
  5. apache2 aliyun https证书配置
  6. Acwing-271-杨老师的照相排列(DP)
  7. java邮箱正则验证
  8. sizeof运算符、字节对齐考点(面宝P50)
  9. JAVA笔记6-继承和权限控制
  10. layer .net中使用