首页 > 解决方案 > Springboot应用程序在运行本地vs包maven时读取不同的路径

问题描述

因此,我正在开发一个应用程序并尝试在主类中使用 @ImportResource 来引用包含 SOAP 服务配置的 xml 文件。当我尝试在本地运行/调试时,它工作得很好,但是当我使用 maven 包构建 jar 时,它会产生 java.io.FileNotFoundException 错误。

这是我的主要课程

@SpringBootApplication
@ImportResource(locations = "jaxwsconfig.xml")
public class SoapApplication {

    public static void main(String[] args) {
        SpringApplication.run(SoapApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new WSSpringServlet(), "/Services");
    }
}

这是我在使用 maven 包时遇到的一些错误

IOException parsing XML document from ServletContext resource [/jaxwsconfig.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/jaxwsconfig.xml]

PS:我对文件路径有疑问。当我没有定义绝对路径时,Java Spring 指的是哪里?就像在这种情况下,当我只定义“jaxwsconfig.xml”时,它指的是哪里?

标签: javaspringmavensoap

解决方案


如果你的 jaxwsconfig.xml 文件在资源文件夹下,即 src/main/resource,你可以直接给它,

@ImportResource({"classpath*:applicationContext.xml"})

如果假设文件存在于资源 src/main/resource/foldername 内的文件夹中。你必须给,

@ImportResource(locations = {"classpath:foldername/application-context.xml"})

推荐阅读