Spring整合Mybatis 扫描注解创建Bean报错的解决方案

目录
  • Spring整合Mybatis 扫描注解创建Bean报错
  • springboot+mybatis使用注解方式,出现错误创建dao层bean

Spring整合Mybatis 扫描注解创建Bean报错

情景:

LZ在整合Spring 和Mybatis 的时候,整合之后部署到tomcat报错

报错信息:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ccl.dao.BaseDao com.ccl.service.impl.BaseService.baseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ccl.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:445)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:419)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:544)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:304)
... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ccl.dao.BaseDao com.ccl.service.impl.BaseService.baseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ccl.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 39 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ccl.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 41 more

之前出现创建Bean的时候错误,

最后发现在applicatetionContext.xml中的扫描注解的语句是:

<!-- 使用注解式注入 -->
 <context:annotation-config />
 <!-- 自动扫描 -->
 <context:component-scan base-package="com.ccl" />

在扫描dao文件的配置项中

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。
   可指定多个包,包与包之间用逗号或分号分隔-->
  <property name="basePackage" value="com.ccl.mapper" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
 </bean>  

LZ在这边写错了,这个配置是扫描dao的,写成了mapper,所以将上面的com.ccl.mapper改为com.ccl.dao,问题解决

springboot+mybatis使用注解方式,出现错误创建dao层bean

今天项目里面的其他控制器都使用jpa注解进行数据的查询。我写的功能模块里面的控制器,要求不用jpa查询数据,使用mybatis的注解查询。

在springboot项目中,写完后启动项目发现错误创建dao层bean,找了很久不知道为啥,最后发现项目中引入的jar包有问题,原来引入的jar包为mybatis和mybatis-spring的。

修改成mybatis-spring-start后,重新启动项目,问题解决了

报错信息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'battleWindController': Unsatisfied dependency expressed through field 'battleWindServiceImpl'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'battleWindServiceImpl': Unsatisfied dependency expressed through field 'battleWindDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.galaxy.metadata.dao.BattleWindDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at com.galaxy.metadata.MetadataApplication.main(MetadataApplication.java:12) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.0.RELEASE.jar:2.1.0.RELEASE]

开始jar包

修改后的jar包

替换包后问题解决

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 解决SpringBoot整合Mybatis扫描不到Mapper的问题

    闲来无事,想学学springboot,开始搭建一个项目,但是一直显示mapper扫描不到的错误: "Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsa

  • SpringBoot整合mybatis常见问题(小结)

    Spring中常见问题 1.NoSuchBeanDefinitionException 2.'..Service' that could not be found service找不到 3.port 80 was already in use 端口号被占用 4.TemplateInputException 模板解析异常或找不到模板 1.检查模板所在的目录是否与配置的前缀目录相同 2.检查返回的模板是否存在,返回值类型是否一致 3.检查配置前缀时是否以"/"斜杠结尾 4.控制层的url与

  • Spring整合MyBatis的三种方式

    1.整合之前的环境准备 导入相关的jar包 Junit测试 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> MyBatis <dependency> <groupId

  • 详解spring+springmvc+mybatis整合注解

    每天记录一点点,慢慢的成长,今天我们学习了ssm,这是我自己总结的笔记,大神勿喷!谢谢,主要代码!! ! spring&springmvc&mybatis整合(注解) 1.jar包 2.引入web.xml文件 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param

  • Spring整合Mybatis 扫描注解创建Bean报错的解决方案

    目录 Spring整合Mybatis 扫描注解创建Bean报错 springboot+mybatis使用注解方式,出现错误创建dao层bean Spring整合Mybatis 扫描注解创建Bean报错 情景: LZ在整合Spring 和Mybatis 的时候,整合之后部署到tomcat报错 报错信息: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name

  • Mybatis collection查询集合属性报错的解决方案

    目录 Mybatis collection查询集合属性报错 错误日志 背景 排查 解决方法 MyBatis 包含属性为集合的查询 父实体类 关联子查询实体类 父查询返回结果实体类映射 集合子查询实体类映射 父查询 子查询 Mybatis collection查询集合属性报错 错误日志 org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, cla

  • Mybatis批量修改时出现报错问题解决方案

    批量修改代码如下 <update id="UPDATE_HOTEL_REAL_TIME_PRICE" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" separator=";"> UPDATE VST_HOTEL_REAL_TIME_PRICE

  • 基于Spring整合mybatis注解扫描是否成功的问题

    目录 Spring整合mybatis注解扫描是否成功 Mybatis注解@MapperScan扫描包报:Invalid bound statement (not found) 总结 Spring整合mybatis注解扫描是否成功 IDEA spring整合mybatis会使用注解扫描的配置 <context:component-scan base-package="/> 不管是使用@ Component.@Service.@Controller还是Repository,都是sprin

  • Spring与Mybatis基于注解整合Redis的方法

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载后直接启动redis-server就行了,见下图: 一,先上jar包 二,创建实体类 package com.sl.user.vo; import java.io.Serializable; import com.fasterxml.jackson.databind.PropertyNamingSt

  • Spring Boot整合mybatis使用注解实现动态Sql、参数传递等常用操作(实现方法)

    前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库操作中的常用操作. 其实,mybatis 注解方式 和 XML配置方式两者的使用基本上相同,只有在构建 SQL 脚本有所区别,所以这里重点介绍两者之间的差异,以及增删改查,参数传递等注解的常用操作. 详解SpringBoot 快速整合Mybatis(去XML化+注解进阶)已经介绍过了,不清楚的朋友可

  • Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例

    前言 距离第一篇 Spring Boot 系列的博文 3 个月了.虽然 XML 形式是我比较推荐的,但是注解形式也是方便的.尤其一些小系统,快速的 CRUD 轻量级的系统. 这里感谢晓春 http://xchunzhao.tk/ 的 Pull Request,提供了 springboot-mybatis-annotation 的实现. 一.运行 springboot-mybatis-annotation 工程 然后Application 应用启动类的 main 函数,然后在浏览器访问: http

  • Spring SpringMVC,Spring整合MyBatis 事务配置的详细流程

    整合思路 (1)SSM是什么? Spring,SpringMVC,Mybastis (2)思路 搭建整合的环境,初始化环境 搭建Spring环境,配置完成并测试 (service层) 再使用Spring整合MyBatis框架,并测试(Dao层) 最后使用Spring整合SpringMVC框架,并测试(web层) SSM搭建环境 (1)数据库创建ssm (2)创建maven工程 (3)git (创建.gitignore来过滤不用提交的文件) (4)依赖框架 (5)log4j.properties

  • Spring整合Mybatis实操分享

    目录 Mybatis的基本工作原理 分析需要解决的问题 Spring中Bean的产生过程 解决问题 总结 在介绍Spring整合Mybatis原理之前,我们得先来稍微介绍Mybatis的工作原理. Mybatis的基本工作原理 在Mybatis中,我们可以使用一个接口去定义要执行sql,简化代码如下: 定义一个接口,@Select表示要执行查询sql语句. public interface UserMapper { @Select("select * from user where id = #

随机推荐