org.springframework.beans.factory.NoSuchBeanDefinitionException:

这个错误困扰了我一宿,今天终于解决了,当我知道为啥的时候心中一万头草泥马飞过。。。
这是全部的错误信息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'iUserMapper' is defined

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:701)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
    at test.com.fanyafeng.service.impl.IUserMapperTest.testFindUserById(IUserMapperTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我从网上搜了好多,回答的五花八门,不过我感觉有可能和mybatis和spring的版本有关系,所以才出现了这种问题,我用的版本是:spring 4.3 mybatis 3.3
出现这个问题的原因是批量扫描mapper配置出现的问题,由于我是初学者,出错的第一反应是上网查答案,而不是看log,这里自我批评一下,然后我仔细查阅了一下log,我把关键的log打出来:

11:17:32.354 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
11:17:32.354 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'dataSource'
11:17:32.374 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'dataSource' to allow for resolving potential circular references
11:17:32.393 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'dataSource'
11:17:32.393 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSessionFactory'
11:17:32.393 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'sqlSessionFactory'
11:17:32.399 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
11:17:32.413 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'sqlSessionFactory' to allow for resolving potential circular references
11:17:32.420 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'dataSource'
11:17:32.421 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'sqlSessionFactory'
11:17:32.551 [main] DEBUG o.m.spring.SqlSessionFactoryBean - Parsed configuration file: 'class path resource [mybatis/Configuration.xml]'
11:17:32.553 [main] DEBUG o.m.spring.SqlSessionFactoryBean - Property 'mapperLocations' was not specified or no matching resources found
11:17:32.554 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'sqlSessionFactory'
11:17:32.554 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.mybatis.spring.mapper.MapperScannerConfigurer#0'
11:17:32.554 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'IUserMapper'
11:17:32.554 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'IUserMapper'
11:17:32.555 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'IUserMapper' to allow for resolving potential circular references
11:17:32.566 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'sqlSessionFactory'
11:17:32.576 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'IUserMapper'
11:17:32.654 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'IUserMapper'

我的配置文件关键代码

<!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/Configuration.xml"/>
        <!--<property name="mapperLocations" value="classpath:com/fanyafeng/mapper/*.xml"/>-->
    </bean>

    <!--扫描mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描包的路径-->
        <property name="basePackage" value="com.fanyafeng.mapper"/>
        <!--注入sqlsession工程bean的名字-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

    </bean>

测试代码(错误的)

package test.com.fanyafeng.service.impl;

import com.fanyafeng.mapper.IUserMapper;
import com.fanyafeng.model.UserModel;
import com.fanyafeng.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

/**
 * Author: fanyafeng
 * Data: 16/10/16 00:02
 * Email: fanyafeng@live.cn
 */
public class IUserMapperTest {
    private ApplicationContext applicationContext;

    @Before
    public void before() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }


    /**
     * Method: findUserById(int id)
     */
    @Test
    public void testFindUserById() throws Exception {
//        UserService userService = (UserService) applicationContext.getBean("userService");
//        UserModel userModel = userService.findUserById(1);

        IUserMapper iUserMapper = (IUserMapper) applicationContext.getBean("iUserMapper");
        UserModel userModel = iUserMapper.findUserById(1);
        System.out.print("输出:" + userModel.toString());
    }
}

我看的教学视频说的是默认的mapper首字母小写,但是,但是,但是log里面新建实例的话并没有做任何修改,就是原mapper的名字所以只要把i改成I就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值