簡體   English   中英

Spring應用程序上下文無法在Maven資源文件夾下找到屬性文件

[英]Spring application context unable to find property files under maven resources folder

在此處輸入圖片說明

我在項目中使用Maven,並將database.properties文件放在maven資源文件夾下。

我在Spring應用程序上下文中訪問它

<context:property-placeholder location="classpath:database.properties" />

但是我不明白為什么我在服務器啟動時收到此文件未找到錯誤。

無法加載屬性; 嵌套的異常為java.io.FileNotFoundException:類路徑資源[database.properties]無法打開,因為它不存在

據我所知,任何資源放置在資源文件夾下,都會由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插件。 這里有一個詳盡的教程:

http://crunchify.com/how-to-create-a-war-file-from-eclipse-using-maven-plugin-apache-maven-war-plugin-usage/

您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM