MyBatis与Spring MVC结合时,使用DAO注入出现:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

摘要:
错误源于使用以下示例:http://www.yihaomen.com/article/java/336.htm,如果操作期间发生以下错误:Invocationofitmethodfailed;嵌套异常是java.lang.IllegalArgumentException:属性'sqlSessionFactor'或'sqlSessionTemplate'

错误源自使用了这个例子:http://www.yihaomen.com/article/java/336.htm,如果运行时会出现如下错误:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

MyBatis与Spring MVC结合时,使用DAO注入出现:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required第1张

以下是解决思路,参考:http://blog.csdn.net/xkhgnc_6666/article/details/52063713说的解决方法,分析如下:

1、从字面上意思理解是注入时导致了混乱,相同类型的Bean不知道是注入SqlSessionFactory还是SqlSessionTemplate。

2、可以使用@Qualifier注解和@Autowired注解通过指定哪一个真正的Bean将会被装配来消除混乱,参考:http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-qualifier-annotation.html,也就是具体指定使用上面的哪一个进行Bean注入。

具体解决代码,在DAO中加入如下代码:

    @Autowired(required = false)
    @Qualifier("sqlSessionFactory") 
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

还可以这样写:

    @Autowired(required = false)
    public void setSqlSessionFactory(
            @Qualifier("sqlSessionFactory")
            SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

效果都是一样的。

免责声明:文章转载自《MyBatis与Spring MVC结合时,使用DAO注入出现:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇小程序定义并使用类Vuex 的项目实例1 项目初始化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mybatis plus 联合查询

在xml中只需要需要写如下的代码即可实现分页: <select parameterType="map" resultType="com.test.mybatisplus.pojo.User"> SELECT <include refid="Base_Column_List" />...

mybatis的关联查询以及count

1.多表查询,1对1的时候,最简单的做法 <resultMap id="postInfo" type="postInfoEntity"> <id property="postId" column="post_id"/> <result property="userName" column="us...

MyBatis 原码解析(version:3.2.7)

mybatis-plus 实践及架构原理.pdf mybatis-plus思维导图 首先,我们看使用原生的JDBC来操作数据库的方式: // 1. 获取JDBC Connection Connection connection = DbManager.getConnectoin(); // 2. 组装sql语句 String sql = "inser...

MyBatis的Example如何按条件排序(Day_35)

MyBatis的Example如何按条件进行排序? 背景:有时我们在使用mybatis example 进行查询时,需要进行相应的业务排序。本博客以下图为例 @Override public List<Paper> viewPaperAll() { PaperExample paperExample = new...

Mybatis3源码笔记(一)环境搭建

1. 源码下载 地址:https://github.com/mybatis/mybatis-3.git。 国内访问有时确实有点慢,像我就直接先fork。然后从git上同步到国内的gitte上,然后在idea上一波clone下来,速度就比较理想了。 2. mybatis-parent 项目依赖mybatis-parent,下载mybatis-parent。...

【原】配置Log4j,使得MyBatis打印出SQL语句

【环境参数】 JDK:jdk1.8.0_25 IDE:Eclipse Luna Servie Release 1 框架:Spring 4.1.5 + SpringMVC 4.1.5 + MyBatis 3.2.2 【配置步骤】 一、设置MyBatis的Setting(非必须,不同环境下,可能不需要该设置)。 在“src/main/java/resource...