【发布时间】: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