时间:2021-05-19
重新创建了一个项目,代码结构有所改变,结果在启动服务时,一直报如下错误
严重:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'car1UserInfoService': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bonade.core.base.BaseMapper<com.bonade.system.car1user.model.Car1UserInfo>' 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:587)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bonade.core.base.BaseMapper<com.bonade.system.car1user.model.Car1UserInfo>' 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.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
... 25 more
思考过程:
1.mybatis jar包增加ok
2.mybatis的xml配置,已经配置
3.其他正常的model类,放过来依然报错,证明不是model类的问题,在装载model时正常
4.根据报错信息:
Error creating bean with name ‘car1UserInfoService': Unsatisfied dependency expressed through field ‘mapper'
找到对应的代码
@Autowired
protected BaseMapper<T> mapper;
发现是装载BaseMapper时报错,那么猜测是BaseMapper没有被加载到spring容器。
此时查看mybatis.xml配置文件
再看看mapper文件路径:
发现配置文件和它对应不上,所以扫描不到mapper类,猜测是这个原因导致报错。所以修改了下
结果启动正常。
但是这样子,对于工程来说,mapper文件的层级就固定死了,所以可以这样子配置:
这里面涉及到一个语法:
1.?代表匹配任意一个字符,* 代表匹配0个或多个任意字符 ,**/匹配任意多个目录
2.对于多个路径,可以用,分割
备注:
改成第二种方式也能解决问题,但是发现启动的速度减慢了,因为扫描的包增多了,需要的时间也延长了。
所以如果能统一一种规范,来进行开发的话,还是用第一种方式。
以上这篇关于mybatis mapper类注入失败的解决方案就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
问题在Service层注入Mybatis的Mapper我们通常会使用@Autowired自动注入@AutowiredprivateProductMapperpr
两种防水都可以,但是使用方式不同。@mapper需要mybatis和mybatis-spring的版本足够的高才可以。不然导入不到@mapper。补充知识:关于
typeAliases在我们开发中,Mapper中的实体类每次都要写上包名,是不是特别的麻烦针对这一现象,myabtis提供了解决方案,简化开发typeAlia
什么是通用Mapper通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就
用过MyBatis3的人可能会觉得为什么MyBatis的Mapper接口没有实现类,但是可以直接用?那是因为MyBatis使用Java动态代理实现的接口。这里仅