nested exception is java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened

來源:互聯網
上載者:User

標籤:工具   src   源檔案   pat   classes   ESS   oid   director   作用   

idea+maven+springmvc項目,做單元測試報錯,找不到設定檔。

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})public class TestMybatis {    private static Logger logger = LoggerFactory.getLogger(TestMybatis.class);    @Resource    private MessageService messageService=null;    @Test    public  void test1(){        Message message=messageService.getMessageById(1);        logger.info("Command====:{}", message.getCommand());    }}

點擊"classpath:spring-mybatis.xml"還能找到檔案。

已經設定檔放在src/main/resouces目錄下,並且設定成Resources Root。但是不起作用,編譯後發現target/classes檔案下沒有該設定檔。

經過網上一番搜尋,解決IDEA的這個問題有兩種方式。

第一種是建立src/main/resources檔案夾,將xml等資源檔放置到這個目錄中。maven工具預設在編譯的時候,會將resources檔案夾中的資源檔一塊打包進classes目錄中。

第二種解決方式是配置maven的pom檔案配置,在pom檔案中找到<build>節點,添加下列代碼:

<build>        <resources>            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>            </resource>        </resources>    </build>

顯然,第一種在這對我不好使。

我看了下pom.xml,因為是項目學習,在網上copy的。發現已經有了一個resource配置,就是上面貼出來。(因為src/main/java下面有mapper.xml,所以做了配置)。

於是我把<resources>節點刪掉,重新編譯,發現src/main/resouces下面的設定檔已經到target/classes檔案夾下。但是不幸的是,之前的mapper.xml不在classes檔案下了。

不過沒關係,我們把兩種都配置在resources即可。

<build>    <resources>         <resource>            <directory>src/main/java</directory>            <includes>                    <include>**/*.xml</include>             </includes>            </resource>            <resource>                <directory>src/main/resources</directory>          </resource>     </resources></build>

 

nested exception is java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.