Coin163

首页 > java - Spring集成mybatis

java - Spring集成mybatis

相关推荐:java开发之netty里集成spring注入mysq连接池(二)

3.实现注入3.1构建applicationContext.xml在src目录下建立applicationContext.xml点击(此处)折叠或打开<xmlversion="1.0"encoding="UTF-8"><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xs

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)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

,

这其实是一个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实例。

相关推荐:java开发之netty里集成spring注入mysq连接池(一)

netty的性能非常高,能达到8000rps以上,见各个web服务器的性能对比测试1.准备好需要的jar包点击(此处)折叠或打开spring.jar//spring包 netty-3.2.4.Final.jar// netty库 commons-dbcp.jar// dbcp数据库连接池 mysql

,

看文档:

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

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

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

,

SqlSessionFactory是一个接口,DefaultSqlSessionFactory是一个类实现了SqlSessionFactory接口。

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

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'sqlSessionFactory' must be of type [org.mybatis.spring.SqlSessionFactoryBean], but was actually of typ...

------分隔线----------------------------