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

 更新时间:2021年10月09日 10:41:40   作者:xadjccl  
这篇文章主要介绍了Spring 整合Mybatis 扫描注解创建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包

替换包后问题解决

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

相关文章

  • SpringBoot 中常用注解及各种注解作用

    SpringBoot 中常用注解及各种注解作用

    本篇文章将介绍几种SpringBoot 中常用注解及各个注解的作用,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-03-03
  • SpringCloud Feign传递HttpServletRequest对象流程

    SpringCloud Feign传递HttpServletRequest对象流程

    HttpServletRequest接口的对象代表客户端的请求,当客户端通过HTTP协议访问Tomcat服务器时,HTTP请求中的所有信息都封装在HttpServletRequest接口的对象中,这篇文章介绍了Feign传递HttpServletRequest对象的流程,感兴趣的同学可以参考下文
    2023-05-05
  • JAVA正则表达式校验qq号码的方法

    JAVA正则表达式校验qq号码的方法

    Java作为一种开发语言,有许多值得推荐的地方,但是它一直以来没有自带对正则表达式的支持。下面小编给大家带来了JAVA正则表达式校验qq号码的方法,需要的朋友参考下吧
    2018-04-04
  • Spring和Mybatis整合的原理详解

    Spring和Mybatis整合的原理详解

    这篇文章主要介绍了Spring和Mybatis整合的原理详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-07-07
  • JAVA中阻止类的继承(官方和非官方)

    JAVA中阻止类的继承(官方和非官方)

    在面向对象的理论中, 有一些方案要求你用一个办法来声明一个不可继承的类。一般而言,如果类提供的功能不应该被改变,或者更恰当的说,是被覆盖(override)的时候才会出现这种情况。在这篇文章里,我讨论在JAVA语言中的实现办法--官方和非官方的办法
    2014-01-01
  • SpringMVC 重新定向redirect请求中携带数据方式

    SpringMVC 重新定向redirect请求中携带数据方式

    这篇文章主要介绍了SpringMVC 重新定向redirect请求中携带数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java使用httpRequest+Jsoup爬取红蓝球号码

    Java使用httpRequest+Jsoup爬取红蓝球号码

    本文将结合实例代码,介绍Java使用httpRequest+Jsoup爬取红蓝球号码,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • 使用webservice自定义注解处理参数加解密问题

    使用webservice自定义注解处理参数加解密问题

    这篇文章主要介绍了使用webservice自定义注解处理参数加解密问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java深入浅出数组的定义与使用上篇

    Java深入浅出数组的定义与使用上篇

    数组是有序的元素序列,若将有限个类型相同的变量的集合命名,那么这个名称为数组名。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。数组是在程序设计中,为了处理方便, 把具有相同类型的若干元素按有序的形式组织起来的一种形式
    2022-03-03
  • 详解Java利用ExecutorService实现同步执行大量线程

    详解Java利用ExecutorService实现同步执行大量线程

    这篇文章主要介绍了Java利用ExecutorService实现同步执行大量线程,ExecutorService可以维护我们的大量线程在操作临界资源时的稳定性。
    2017-03-03

最新评论