首页 > 解决方案 > 访问资源文件夹中的文件

问题描述

我正在开发一个 Spring Boot 应用程序,我想将 application.yml 中的一个参数(jndi.path)的值设置为资源文件夹中文件的路径。根据环境不同,路径也不同。

在我的 application.yml 中,我有这样的东西:


spring:
    profiles: local
    jndi:
        path: file:C/path/to/myRessource/dev
spring:
    profiles: rct
    jndi:
        path: file:C/path/to/myRessource/rct

dev并且rct是资源文件夹中的文件夹。唯一可行的方法是设置绝对路径。

(原谅我的英语)

标签: javaspringspring-bootspring-mvc

解决方案


在资源中创建一个属性文件(config.properties)并在此文件中提供此配置文件值,并在 yml 文件中使用占位符,如下所示:

profile=rct/dev

并在 yml 文件中使用如下:

spring:
profiles: ${profile}
jndi:
    path: file:C/path/to/myRessource/rct

您必须在 yml 中添加的另一项更改是提供属性文件的详细信息,可以通过这种方式完成。

    config:
       config-file-path: config.properties

推荐阅读