idea maven spring xml文件找不到 未编译

问题描述:在使用idea学习spring的ioc时,我想用applicationContext.xml引入其他的配置文件(hello.xml),hello.xml是放在单独的java包中的。文件目录:

idea maven spring xml文件找不到 未编译

我在applicaContext.xml中这样写

idea maven spring xml文件找不到 未编译

hello.xml中这样写

idea maven spring xml文件找不到 未编译

HelloWorldTest.java

idea maven spring xml文件找不到 未编译

进行单元测试,报错显示该xml未找到。打开项目文件显示:

idea maven spring xml文件找不到 未编译

因此可以看出在运行的时候idea并未将xml编译到target目录下。经过搜索找到了解决方法:在pom.xml中<build></build>内添加一段代码:

    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>*</include>
          <include>*/*</include>
        </includes>
        <filtering>true</filtering>
      </resource>
      <!--xml编译到源码下面 -->
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>

代码比较简单易懂,就是将java文件夹下的java文件和xml文件都自动编译到target目录下。这样再使用classpath就能找到了

idea maven spring xml文件找不到 未编译