spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext...
生活随笔
收集整理的這篇文章主要介紹了
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);@Resourceprivate CreditService creditService;@Resourceprivate TeamService teamService;@Testpublic 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>
?
轉載于:https://www.cnblogs.com/xionggeclub/p/6961285.html
總結
以上是生活随笔為你收集整理的spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: log4net报错集
- 下一篇: 网页背景