spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext
最近遇到一个奇怪的问题。maven项目再进行junit单元测试的时候发现无法加载配置文件。一会能加载一会又不能加载。然后试了在src/main/resource下面的配置文件放到src/test/resource下,这样每次都能加载了。
但是理论上不用放在test下也是可以加载的。
package com.xsw.test;
import javax.annotation.Resource; import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.alibaba.fastjson.JSON;
import com.xsw.model.TeamsDTO;
import com.xsw.service.CreditService;
import com.xsw.service.TeamService; @RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = { "classpath:spring.xml" ,"classpath:spring-mybatis.xml"})
public class MybatisTest { private static Logger logger = Logger.getLogger(MybatisTest.class);
@Resource
private CreditService creditService;
@Resource
private TeamService teamService;
@Test
public void testTeam(){
TeamsDTO cre = teamService.getTeamById(1L);
logger.info(JSON.toJSON(cre));
}
}


后来发现用eclipse进行编译的时候 指向maven install 有时mapping文件和配置文件无法编译到target目录下。为什么老是有这种偶然现象呢?
为了每次都能编译正确,在pom下强制配置 进行编译 配置如下。放在最后
</build>
</project>
之前就可以了
<!--编译之后包含xml-->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext的更多相关文章
- 集成JUnit测试错误java.lang.IllegalStateException: Failed to load ApplicationContext
1 详细错误信息 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.t ...
- junit测试时,出现java.lang.IllegalStateException: Failed to load ApplicationContext
课程设计要求进行junit测试,我是在已经做好的ssh项目上做的测试,测试类代码如下 package com.zhang.web.services; import static org.junit.A ...
- Junit Test 的时候出错java.lang.IllegalStateException: Failed to load ApplicationContext
问题原因 JDK1.8 spring版本3.2.0RELEASE JDK和spring版本不兼容 解决方法 1.降低JDK版本到1.7 2.将spring的版本升级到4.0.0RELEASE或者 ...
- idea启动spring boot无法加载或找不到主类
问题产生原因:moudle名称修改,导致项目启动不了 在Terminal界面中执行以下三个命令,我在执行第一个命令的时候报了一个找不到dependency的错误,把那个报错的dependency删了就 ...
- Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
1 报错描述 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @Boot ...
- spring中junit 提示Failed to load ApplicationContext
错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext 2:Error creating bean wit ...
- junit单元测试报错Failed to load ApplicationContext,但是项目发布到tomcat浏览器访问没问题
junit单元测试报错Failed to load ApplicationContext,但是项目发布到tomcat浏览器访问没问题,说明代码是没问题的,配置也没问题.开始时怀疑是我使用junit版本 ...
- 2 Struts2的执行流程&配置文件的加载顺序
执行流程: 访问前段页面,通过url访问action 访问xml中Struts2核心过滤器,并执行一组拦截器(这组拦截器在struts-default.xml中,实现了部分功能) 通过action配置 ...
- 【spring boot】【elasticsearch】spring boot整合elasticsearch,启动报错Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8
spring boot整合elasticsearch, 启动报错: Caused by: java.lang.IllegalStateException: availableProcessors ], ...
随机推荐
- springMVC中@RequestParam和@RequestBody注解的用法
springMVC中@RequestParam注解用在Controller层获解析.提取参数,当然你也可以用request.getParameter("name")来获取参数,而@ ...
- html5中的新标签
header <header> 标签定义文档的页眉(介绍信息). nva 根据W3C的定义规范:nav元素是一个可以用来作为页面导航的链接组: <nav><ul>& ...
- grep运用
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- Nginx Https配置不带www跳转www
把 morethink.cn和www.morethink.cn合并到一个server上去,使用301永久重定向. 然后将 https://morethink.cn 转到 https://www.mor ...
- JAVA中的数据存储空间简述
在 JAVA 中,有六个不同的地方可以存储数据: 1. 寄存器( register ): 最快的存储区,因为它位于不同于其他存储区——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进 ...
- How to create and start VirtualBox VM without GUI
Suppose you want to create and run virtual machines (VMs) on VirtualBox. However, a host machine doe ...
- Go笔记-垃圾回收集和SetFinalizer
[垃圾回收] 1- Go的开发者也不用写代码来释放程序中不再使用的变量和结构占用内存,Go中有独立的进程,垃圾回收器(GC),处理这些事情.它会搜索不再使用的变量然后释放它们. 2- ...
- POJ [P2289] Jamie's Contact Groups
二分+二分图多重匹配 辣鸡ACM式读入 对于这种奇葩的读入方法,还是老老实实的用scanf吧 #include <iostream> #include <cstdio> #in ...
- 学习H5仿制网站时遇到的问题
学习H5的第二周,开始仿制网站,在本过程中也发现了自己存在的更多问题. 1.TOP块:在制作top中,遇到的第一个问题,一旦top左右的背景色不同该怎样去调整 解决方式:建立两个div取代一个div ...
- 【IT人】如何提高阅读源代码的效率
1.最近刚到公司,公司就发一架构代码自己看,看了几天看的想吐,也在网上找了下相关的技巧吧,不是有句话叫做:成功必有方法,失败总是借口! 2.借鉴别人的方法来看看如下: 记得在开源流行之前,我看过的代码 ...