“名为XXX的bean必须是[XXX]类型,但实际上是[XXX]类型". [英] "Bean named XXX must be of type[XXX], but was actually of type[XXX]"

查看:79
本文介绍了“名为XXX的bean必须是[XXX]类型,但实际上是[XXX]类型".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我整合spring和mybatis时,遇到错误输出,说:

when I integrate spring and mybatis, I encountered a error output, saying that:

名为"sqlSessionFactory"的Bean必须为[org.mybatis.spring.SqlSessionFactoryBean]类型,但实际上为[org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] ​​

Bean named 'sqlSessionFactory' must be of type [org.mybatis.spring.SqlSessionFactoryBean], but was actually of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory]

这是我的代码段:

ApplicationContext context = new ClassPathXmlApplicationContext("spring_mybatis_integration/spring_config.xml");
    SqlSessionFactoryBean sqlSessionFactoryBean = context.getBean("sqlSessionFactory", org.mybatis.spring.SqlSessionFactoryBean.class);

这是我在xml中的bean定义:

here is my bean definition in xml:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="configLocation" value="spring_mybatis_integration/mybatis_config.xml"></property>
  <property name="dataSource" ref="dataSource"></property>
</bean>

如您所见,

在Java代码和xml文件中,我都将bean sqlSessionFactory与类org.mybatis.spring.SqlSessionFactoryBean关联在一起,为什么错误输出告诉我另一个非相对类名org.apache.ibatis.session.defaults.DefaultSqlSessionFactory?

as you can see, both in java code and in xml file, I associate the bean sqlSessionFactory with class org.mybatis.spring.SqlSessionFactoryBean, why does the error output tell me another non-relative class name org.apache.ibatis.session.defaults.DefaultSqlSessionFactory?

非常感谢!

  • mybatis 3.1.1
  • 春季3.2.0
  • mybatis-spring 1.1.1

推荐答案

通过依赖注入来访问SqlSessionFactoryBean是没有意义的,通常我们使用的是Factory Bean创建的对象,而不是Factory Bean本身.工厂Bean返回DefaultSqlSessionFactory实例的情况.

There is no point to accessSqlSessionFactoryBean via dependency injection, normally we work with the objects created by the Factory Beans, not the Factory Beans themselves, in this case the Factory Bean returns a DefaultSqlSessionFactory instance.

请参见但是,如果您确实要访问FactoryBean实例,则应使用&符号&参见

But if you really want to access the FactoryBean instance, you should use ampersand symbol & see Spring: Getting FactoryBean object instead of FactoryBean.getObject()

是的,返回工厂的Factory Bean的概念可能有点令人困惑,但这就是Spring的工作方式.

Yes, the concept of the Factory Beans that return factories may be a bit confusing, but that is how things work in Spring.

所以您想要的可能是SqlSessionFactory而不是SqlSessionFactoryFactoryBean.

So it's likely SqlSessionFactory instead of the SqlSessionFactoryFactoryBean is what you want.

更新:实际上,MyBatis甚至在 SqlSessionFactoryBean的文档中对此进行了解释.

update: actually MyBatis even explained this in the documentation on SqlSessionFactoryBean

请注意,SqlSessionFactoryBean实现了Spring的FactoryBean 接口(请参阅Spring文档的3.8节).这表示 Spring最终创建的bean不是 SqlSessionFactoryBean本身,但是工厂返回的结果 工厂中的getObject()调用.在这种情况下,Spring将 在应用程序启动时为您构建一个SqlSessionFactory并将其存储 名称为sqlSessionFactory

Note that SqlSessionFactoryBean implements Spring's FactoryBean interface (see section 3.8 of the Spring documentation). This means that the bean Spring ultimately creates is not the SqlSessionFactoryBean itself, but what the factory returns as a result of the getObject() call on the factory. In this case, Spring will build an SqlSessionFactory for you at application startup and store it with the name sqlSessionFactory

这篇关于“名为XXX的bean必须是[XXX]类型,但实际上是[XXX]类型".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆