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
实例。

TA贡献1784条经验 获得超2个赞
看文档:
问题在于,SqlSessionFactoryBean
是个工厂,容器通过这个工厂的getObject()
方法得到了真正要注入的Bean
。我猜你应该是在messageLogDao
之类要使用sqlSessionFactory
的地方,给注入的内容写死了类型SqlSessionFactoryBean
吧?
但按照我们刚才说的,实际的类型并不是SqlSessionFactoryBean
,应该是DefaultSqlSessionFactory
才对
这个你得多看文档。我也没测试,不敢保证结果^^

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