bug记录----- resources ----下的.xml配置文件找不到

本文详细解析了Maven项目中遇到的Spring MVC配置文件spring-mvc.xml无法被找到的问题,介绍了如何通过修改pom.xml中的资源配置来确保src/main/resources目录下的XML文件能够正确打包,避免此类错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常:
Caused by: java.io.FileNotFoundException: class path resource [spring-mvc.xml] cannot be opened because it does not exist java.io.FileNotFoundException: class path resource [spring-mvc.xml] cannot be opened because it does not exist

关于在maven项目中出现的如上的bug,springmvc配置文件找不到的情况,但是在target中又是有的 :

在这里插入图片描述
且web.xml 文件中的classpath有没有写错的情况:
在这里插入图片描述
原因:
maven默认扫描src/main/java中的文件而不理会src/main/resources中的xml文件,所以后来添加了resource节点,这样就将src/main/resources中的xml文件改变maven默认的扫描策略,防止造成src/main/resources下的配置文件打包丢失,具体的代码如下:
在pom.xml文件中加入:

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
 

可解决:

参考博客:https://blog.youkuaiyun.com/weixin_42696130/article/details/81074317

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值