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


    spring整合mybatis,在dao层我们只写一个接口,配置相应的*mapper.xml文件,

    报如下错误:

     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)}
     2     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
     3     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
     4     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
     5     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
     6     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
     7     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
     8     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
     9     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    10     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    11     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    12     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    13     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    14     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    15     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
    16     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
    17     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    18     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
    19     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
    20     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    21     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    22     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    23     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    24     at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702)
    25     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    26     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    27     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    28     at java.lang.reflect.Method.invoke(Method.java:498)
    29     at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    30     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    31     at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    32     at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
    33     at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
    34     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    35     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    36     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    37     at java.lang.reflect.Method.invoke(Method.java:498)
    38     at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    39     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    40     at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    41     at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    42     at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    43     at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    44     at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    45     at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    46     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    47     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    48     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    49     at java.lang.reflect.Method.invoke(Method.java:498)
    50     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
    51     at sun.rmi.transport.Transport$1.run(Transport.java:200)
    52     at sun.rmi.transport.Transport$1.run(Transport.java:197)
    53     at java.security.AccessController.doPrivileged(Native Method)
    54     at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    55     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    56     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    57     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    58     at java.security.AccessController.doPrivileged(Native Method)
    59     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    60     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    61     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    62     at 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)}
    64     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    65     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    66     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    67     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    68     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    69     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    70     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    71     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    72     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    73     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    74     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    75     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    76     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    77     at 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)}
    80     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    81     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    82     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    83     at 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="com.xxx.xxx.dao.**.*" />
    3          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    4 </bean>

    basePackage的value应该是指定包名,而我指定的文件名。

    改成如下配置,编译正常通过:

    1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    2          <property name="basePackage" value="com.xxx.xxx.dao" />
    3          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    4 </bean>
  • 相关阅读:
    [日常训练]大灾难
    [cf235D]Graph Game
    [日常训练]选课
    [日常训练]挂科
    [学习笔记]概率&期望
    [日常训练]yayamao的神题
    [学习笔记]原根
    LOJ#2132. 「NOI2015」荷马史诗
    LOJ#2131. 「NOI2015」寿司晚宴
    LOJ#2129. 「NOI2015」程序自动分析
  • 原文地址:https://www.cnblogs.com/feiyujun/p/7725319.html
Copyright © 2020-2023  润新知