[英]Spring boot web war - web-inf/classes/context.xml file not found
當我將基於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>
我正在為此項目創建戰爭檔案。 誰能指導我解決此問題? 謝謝
@ImportResource("classpath:persistence-context.xml")
只要不要忘記將persistence-context.xml
放在webapp文件夾下即可正常工作,因為在Web Project的情況下,它被視為類路徑。
很好地將文件放在src/main/resources
然后嘗試以這種方式讀取它,在這種情況下您將永遠不會遇到路徑問題。
String pathOfTheResurceFile = Thread.currentThread().getContextClassLoader()
.getResource("yourFileName").getPath();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.