我在我的项目中使用maven并且我已经将database.properties
文件放在下面
Maven 资源文件夹。
我正在 spring 应用程序上下文中访问它
<context:property-placeholder location="classpath:database.properties" />
但我不明白为什么我在服务器启动时收到此文件未找到错误。
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [database.properties] cannot be opened because it does not exist
据我所知,无论资源放在资源文件夹下,它都会由 maven 自动添加到类路径中。
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>myapp</finalName>
</build>
</project>
注意: eclipse luna 的 M2E 插件似乎存在一些问题。在 Juno 中执行相同的代码并且运行良好。
最佳答案
这不足以使用 Maven 构建可部署的 war 文件。你至少需要 war 插件。这里有一个完整的教程:
您的 pom 将如下所示,您需要对其运行 mvn clean install 。
我还看到你有一个 spring 应用程序,pom 文件中的 spring 依赖项在哪里?
<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>CrunchifyTutorial</groupId>
<artifactId>CrunchifyTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
关于java - Spring应用程序上下文无法在maven资源文件夹下找到属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31080703/