日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext...

發布時間:2025/3/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。