【问题标题】:Spring boot web war - web-inf/classes/context.xml file not foundSpring Boot web 战争 - web-inf/classes/context.xml 文件未找到
【发布时间】:2014-08-08 12:19:56
【问题描述】:

当我将基于 Spring Boot 的项目部署到 tomcat 时,服务器无法找到 WEB-INF/classes 下的 persistence-context.xml。我在 tomcat 控制台上收到以下错误

 java.io.FileNotFoundException: Could not open ServletContext resource [/persistence-context.xml]

我有以下 Spring boot WS 应用程序的结构:

src/main/java/hello/
                   Application.java
                   HelloWebXml.java

src/main/resources/
                   persistence-context.xml

以下是我的课程

@ComponentScan("foo.bla.bar")
@ImportResource("classpath:persistence-context.xml")
@Configuration
@EnableAutoConfiguration
public class Application {

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

以下是 WebXml 类。

public class HelloWebXml extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
    return application.sources(Application.class);
}
}

以下是我的pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.foo.bar</groupId>
<artifactId>bar-ws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.4.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<properties>
    <start-class>bla.bar.Application</start-class>
</properties>

<build>
    <plugins>
        <plugin> 
            <artifactId>maven-compiler-plugin</artifactId> 
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

我正在构建这个项目的战争文件。谁能指导我如何解决这个问题? 谢谢

【问题讨论】:

  • 你只需要将资源指定为classpath:persistence-context.xml
  • 添加类路径不会产生差异。同样的错误。
  • 请发布您的pom.xml。另外,您如何构建 .war?
  • 我已经添加了 pom.xml。请看一看。谢谢
  • 你需要把persistence-context.xml放到webapp或者web-content文件夹下然后使用classpath:persistence-context.xml

标签: java spring tomcat


【解决方案1】:

@ImportResource("classpath:persistence-context.xml")

可以正常工作,只是不要忘记将persistence-context.xml 放在 webapp 文件夹下,因为它被视为 Web 项目的类路径。

最好将文件放在src/main/resources 下,然后尝试以这种方式阅读它,在这种情况下您将永远不会遇到路径问题。

String pathOfTheResurceFile = Thread.currentThread().getContextClassLoader()
                                    .getResource("yourFileName").getPath();

【讨论】:

  • ... 他们为什么要这样做?请解释为什么会这样
  • ImportResources 通常是红色到 src/main/resources 将 /makes 设置为搜索其内容的目录
  • 它不会产生任何差异。还是一样的错误。
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 2011-06-11
  • 2016-10-02
  • 2018-11-03
  • 1970-01-01
  • 2022-10-14
  • 2020-02-11
  • 2011-01-31
相关资源
最近更新 更多