为了账号安全,请及时绑定邮箱和手机立即绑定

Spring集成mybatis

Spring集成mybatis

胡说叔叔 2019-03-22 19:14:39
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'sqlSessionFactory' must be of type [org.mybatis.spring.SqlSessionFactoryBean], but was actually of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:375)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:969)at com.zhwl.msm.test.Tests.testMessage(Tests.java:20)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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
查看完整描述

3 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

这其实是一个Spring容器BeanFactory的问题,具体可以参考Spring参考7.8.3章节中的这一段,

When you need to ask a container for an actual FactoryBean instance itself instead of the bean it produces, preface the bean’s id with the ampersand symbol ( &) when calling the getBean() method of the ApplicationContext. So for a given FactoryBean with an id of myBean, invoking getBean("myBean") on the container returns the product of the FactoryBean; whereas, invoking getBean("&myBean") returns the FactoryBean instance itself.

大致意思翻译过来就是,当你用beanName去引用一个实现了FactoryBean的工厂实例,其实引用的并不是工厂本身,而是工厂中产生的产品(FactoryBean#getObject())。但是当你真正想要的是工厂而不是产品时,可以在beanName前加上&

也就是,在你的代码中,需要加原先的ref="sqlSessionFactory"改为ref="&sqlSessionFactory"就能获取到类型为SqlSessionFactoryBean而不是SqlSessionFactory的对象了。

题外话,就你的DAO设计来看,依赖一个专为Spring使用的对象并不是一个很好的设计。应该依赖于MyBatis直接提供的SqlSessionFactory实例。


查看完整回答
反对 回复 2019-04-21
?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

看文档:

https://img1.sycdn.imooc.com//5cbbd5cc0001d96808000294.jpg

问题在于,SqlSessionFactoryBean是个工厂,容器通过这个工厂的getObject()方法得到了真正要注入的Bean。我猜你应该是在messageLogDao之类要使用sqlSessionFactory的地方,给注入的内容写死了类型SqlSessionFactoryBean吧?

但按照我们刚才说的,实际的类型并不是SqlSessionFactoryBean,应该是DefaultSqlSessionFactory才对

这个你得多看文档。我也没测试,不敢保证结果^^


查看完整回答
反对 回复 2019-04-21
?
三国纷争

TA贡献1804条经验 获得超7个赞

SqlSessionFactory是一个接口,DefaultSqlSessionFactory是一个类实现了SqlSessionFactory接口。
https://img1.sycdn.imooc.com//5cbbd5d8000100f208000242.jpg

我们知道sqlsession对应着一次数据库会话。由于数据库会话不是永久的,因此sqlsession的生命周期也不应该是永久的.相反,在你每次访问数据库时都需要创建它(当然并不是说在Sqlsession里只能执行一次sql,你可以执行多次,当一旦关闭了Sqlsession就需要重新创建它)。创建Sqlsession通过SqlsessionFactory来创建。由于SqlSessionFactory在mybatis的默认实现类为DefaultSqlSessionFactory , 其构造过程主要是通过调用build方法来创建,build方法通过读取字节流InputStream或者字符流Reader,当然字节流和字符流都是通过读取配置文件xml获取得到的。

https://img1.sycdn.imooc.com//5cbbd5db00010f9608000237.jpg



查看完整回答
反对 回复 2019-04-21
  • 3 回答
  • 0 关注
  • 533 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信