【问题标题】:Referring to resource files in Spring application.properties在 Spring application.properties 中引用资源文件
【发布时间】:2017-02-01 23:30:18
【问题描述】:

我有一个带有 POM 文件的 spring 项目,其中的片段如下 -

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <webResources>
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                        <includes>
                            <include>**/*.csv</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

我的源目录 src/main/webapp 有一个名为 myfile.csv 的 csv 文件。

在执行 mvn 全新安装时,此 csv 文件将存储在我的 target/myproject-0.2.0-SNAPSHOT 目录中。

我需要在 application.properties 文件中使用什么字符串才能到达该文件的位置?当应用程序部署为 war 文件时,此值是否也适用。另外,理想情况下,资源是否应该放在 WEB-INF 目录下,如果是,我需要在我的 POM 文件中进行哪些更改。

【问题讨论】:

    标签: spring properties


    【解决方案1】:

    将文件复制到 WEB-INF/classes。之后它们应该在类路径中可用

    this.getClass().getResourceAsStream("/SomeTextFile.csv");
    

    【讨论】:

    • 非常感谢。所以在 application.properties 文件中,我可以简单地使用这个 - myfile.path = /someTextFile.csv 吗?
    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2023-03-07
    • 2014-02-23
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多