好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决Maven项目加载spring bean的配置xml文件会提示找不到问题

Maven 加载spring bean的配置xml文件会提示找不到

如果你也在开发spring项目时用的是maven项目,如果出现运行是:

***xml can not open ,because it does not exist.

解决方法

很简单,因为maven需要将你的配置文件即***.xml放到根目录下,就是/src/main/java/这个目录下。

如果你把配置文件放到了自己新建的config文件夹中,记住也要放到这个目录里面,然后在

?

1

ApplicationContext ctx = new ClassPathXmlApplicationContext( "config/applicationContext.xml" );

写上路径即可。

如图所示:

Maven 无法找到 xml文件或 properties文件等配置文件

在初次使用 Maven 项目的时候总是会遇到一些比较奇异的问题

就比如说总是会报错:项目里的 **.xml 或 **.properties 配置文件无法找到

这时你去项目中的 classes 生成文件(target 或 out)中找,确实这些配置文件没有被编译

这是因为 Maven 通常会忽略掉标记为 Sources 的文件夹中的配置文件

这是 Maven 项目的目录结构:

有两种解决方案:

一、 将配置文件放入 resources 文件夹中

二、在 Maven 的配置文件 pom.xml 文件中添加以下代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

< build >

         < resources >

             < resource >

                 < directory >src/main/java</ directory >

                 < includes >

                     < include >**/*.properties</ include >

                     < include >**/*.xml</ include >

                 </ includes >

                 < filtering >true</ filtering >

             </ resource >

             < resource >

                 < directory >src/main/resources</ directory >

                 < includes >

                     < include >**/*.properties</ include >

                     < include >**/*.xml</ include >

                 </ includes >

                 < filtering >true</ filtering >

             </ resource >

         </ resources >

     </ build >

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/hubin1989/article/details/51407618

查看更多关于解决Maven项目加载spring bean的配置xml文件会提示找不到问题的详细内容...

  阅读:19次