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

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>

原文地址:https://www.cnblogs.com/xingxing0521/p/9416400.html

时间: 2024-08-12 10:52:10

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

nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog

spring单元测试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/spring-datasource-mogon.xml]; nested exception is java.io.FileNotFoundException: class path resource [

parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not e

控制台异常: parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not exist 报错原因是 xml名字可能写错了 原文地址:https://www.cnbl

intellij idea中出现class path resource [spring/applicationContext.xml] cannot be opened because it does not exist

log4j:WARN No appenders could be found for logger (org.springframework.core.io.support.PathMatchingResourcePatternResolver).log4j:WARN Please initialize the log4j system properly. java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructor

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope

1.错误描述 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.DefaultTestContext.

解决CXF的java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist

以下是错误信息 九月 25, 2017 8:22:04 下午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 信息: Refreshing Root WebApplicationContext: startup date [Mon Sep 25 20:22:04 CST 2017]; root of context hierarchy 九月 25, 2017 8:22:04 下午 or

解决 java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

前几天好好运行的web项目突然跑不起来了 报这个错误 2019-09-16 19:18:29 [ RMI TCP Connection(3)-127.0.0.1:357 ] - [ ERROR ] Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource

Caused by: java.io.FileNotFoundException: class path resource [mappers/*.xml] cannot be opened because it does not exist

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsa

nested exception is java.io.FileNotFoundException: Could not open ServletContext resource

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 注意这段配置尽量写在靠前的位置,因为在tomcat启动后web项目会先初始化上下文,我们这段配置正是指定了上下文配置的文件位置,在上下文初始化完成之后才应当继

出错: IOException parsing XML document from ServletContext resource [/cn.mgy.conig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/cn.mgy.conig]

错误的详细内容: 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/cn.mgy.conig]; nested exception is java.io.FileNotFoundException: Could not open Se