请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

做单元测试的时候报了java.lang.IllegalStateException: Failed to load AplicationContex的错误

老师,我在做单元测试的时候报了java.lang.IllegalStateException: Failed to load AplicationContex的错误
我知道是xml文件错了,但是配置xml的文件那么多,怎么才能定位到出问题的那一个呢,我感觉这是新手都会存在的问题,xml文件太多,基础知识不够,很难分清各自的具体分工

正在回答 回答被采纳积分+3

4回答

提问者 慕后端9028910 2020-03-20 20:00:43

老师,我按照您说的检查了spring-dao和mybatis-config.xml文件的内容,按照您的视频核对了几遍,并且classpath也是按照您的视频设置的,我把图贴了出来,另外按照您说的第三条改成classpath*也试了,都是不行,实在找不到原因了https://img1.sycdn.imooc.com/szimg/5e74b0610879331514401080.jpg

0 回复 有任何疑惑可以回复我~
  • 提问者 慕后端9028910 #1
    可以了老师,我后来把您写的直接复制过来,就好了,还是我写的有问题,我找找是哪里写错了,感谢您的耐心指导
    回复 有任何疑惑可以回复我~ 2020-03-20 20:22:27
提问者 慕后端9028910 2020-03-18 11:11:34

https://img1.sycdn.imooc.com//szimg/5e7191500833d8e410801440.jpg老师这是我的目录结构

0 回复 有任何疑惑可以回复我~
翔仔 2020-03-17 00:24:22

同学好,xml并不多,可能语气有点重,但是请理解这是对你的期望。这我理解并不能用新手来作为推辞,谁都是从新手进来的,也都得通过自己努力来理解透彻。课程里的xml分得已经算是很清晰了,根据三层结构,

controller - spring-web.xml    

service- spring-service.xml  

dao-  spring-dao.xml

log - logback.xml

mybatis -   mybatis-config.xml

都是一一对应的,写在一起才混乱。

同学可以看看你这里

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/spring-dao.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classspath:mybatis-config.xml] cannot be opened because it does not exist

异常已经非常清晰指出spring/spring-dao.xml这个文件里有错,错误是因为mybatis-config.xml  这个视频里创建起来的mybatis配置文件并没有存在,但是你却去读取了。。

所以耐心看看异常 看看caused by的信息,其实很多问题就能解决了,所以遇到问题不要心急,要尝试去定位去解决,这才能体现出我们的价值,我们的天命本来就是要解决问题,如果总看问题望洋兴叹,那我们做这行就失去竞争力了,所以一定要沉得住气,要学会快速成长,要认真学习并理解,而不是黑盒去敲代码编程

0 回复 有任何疑惑可以回复我~
  • 提问者 慕后端9028910 #1
    老师但是我检查了我的文件结构,里面是有mybatis-config.xml这个文件的,依然显示有这个错误,这地方我就搞不明白了
    回复 有任何疑惑可以回复我~ 2020-03-17 10:10:45
  • 提问者 慕后端9028910 #2
    而且我按照网上的检查了编译目录,发现mybatis-config.xml是存在的
    回复 有任何疑惑可以回复我~ 2020-03-17 10:36:26
  • 翔仔 回复 提问者 慕后端9028910 #3
    程序说1是1,不可能骗同学的,这个异常就说的是这个文件不存在,同学可以提供文件目录截图出来,或者和视频对比一下mybatis-config.xml的存放位置,绝对没在classpath下面,或者是eclipse的tomcat没有clean,缓存没是最新的
    回复 有任何疑惑可以回复我~ 2020-03-18 02:11:38
提问者 慕后端9028910 2020-03-16 21:26:59

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/spring-dao.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classspath:mybatis-config.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:826)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:243)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    ... 25 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [classspath:mybatis-config.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:450)
    at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:424)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)
    ... 40 common frames omitted
21:20:03.603 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@57855c9a testClass = AreaDaoTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3b084709 testClass = AreaDaoTest, locations = '{classpath:spring/spring-dao.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信