解决spring mybatis 整合后mapper接口注入失败

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述解决spring mybatis 整合后mapper接口注入失败相关的知识,希望能为你提供帮助。
spring整合mybatis,在dao层我们只写一个接口,配置相应的*mapper.xml文件,
【解决spring mybatis 整合后mapper接口注入失败】报如下错误:

1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloController‘: Unsatisfied dependency expressed through field ‘helloService‘; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloService‘: Unsatisfied dependency expressed through field ‘helloMapper‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aopeng.quest.dao.IHelloMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) 3at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 4at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) 5at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) 6at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 7at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 8at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 9at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 10at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 11at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 12at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) 13at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) 14at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 15at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) 16at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) 17at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 18at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727) 19at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189) 20at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 21at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) 22at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) 23at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) 24at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702) 25at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 26at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 27at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 28at java.lang.reflect.Method.invoke(Method.java:498) 29at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 30at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 31at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 32at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) 33at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) 34at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 35at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 36at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 37at java.lang.reflect.Method.invoke(Method.java:498) 38at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 39at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 40at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 41at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) 42at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) 43at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) 44at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) 45at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) 46at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 47at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 48at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 49at java.lang.reflect.Method.invoke(Method.java:498) 50at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324) 51at sun.rmi.transport.Transport$1.run(Transport.java:200) 52at sun.rmi.transport.Transport$1.run(Transport.java:197) 53at java.security.AccessController.doPrivileged(Native Method) 54at sun.rmi.transport.Transport.serviceCall(Transport.java:196) 55at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) 56at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) 57at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) 58at java.security.AccessController.doPrivileged(Native Method) 59at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) 60at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 61at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 62at java.lang.Thread.run(Thread.java:745) 63 Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloService‘: Unsatisfied dependency expressed through field ‘helloMapper‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aopeng.quest.dao.IHelloMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 64at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) 65at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 66at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) 67at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) 68at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 69at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 70at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 71at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 72at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 73at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 74at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) 75at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) 76at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 77at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 78... 60 more 79 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aopeng.quest.dao.IHelloMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 80at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) 81at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 82at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 83at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 84... 73 more

 
起始这是由于一个低级错误引起的,在applicationContext.xml中有如下的配置:
1 < bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 2< property name="basePackage" value="https://www.songbingjia.com/android/com.xxx.xxx.dao.**.*" /> 3< property name="sqlSessionFactoryBeanName" value="https://www.songbingjia.com/android/sqlSessionFactory" /> 4 < /bean>

 
basePackage的value应该是指定包名,而我指定的文件名。
改成如下配置,编译正常通过:
1 < bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 2< property name="basePackage" value="https://www.songbingjia.com/android/com.xxx.xxx.dao" /> 3< property name="sqlSessionFactoryBeanName" value="https://www.songbingjia.com/android/sqlSessionFactory" /> 4 < /bean>

 

    推荐阅读