SpringBoot Mybatis注入异常

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011687186/article/details/56831270

    这几天在研究Spring Boot框架,早Spring Boot和Mybaits结合的时候,我在Action层面住入Service的代码没有报错,但是在Service注入Mybatis的Mapper接口的时候,报了如下的错误。

java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
[ERROR] 09:51:11 o.s.boot.SpringApplication => Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iUserService': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.company.test.dao.UserMapper 
com.company.test.service.impl.UserServiceImpl.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.company.test.dao.UserMapper] 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:334)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
	at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
	at com.company.test.Application.main(Application.java:19)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field:
 private com.company.test.dao.UserMapper com.company.test.service.impl.UserServiceImpl.userMapper; nested exception is org.springframework.beans.factory
.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.test.dao.UserMapper] found for dependency: expected at least 1 bean which 
qualifie
s 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:573)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues
(AutowiredAnnotationBeanPostProcessor.java:331)
错误提示

UserMapper的接口没有实现类,然后我就看了  我的Spring与Mybatis的Jar包也引入了,配置也配置了,怎么会报这样的错误。


解决方法:

后来发现Spring Boot的main启动方法,默认扫描的是当前包下的子包类,而我的base项目和test项目包的路径不对,当我修改包路径后,

Spring Boot就可以自动扫描到配置文件,Mapper同时也注入了。


没有更多推荐了,返回首页