【问题标题】:Java Spring reference PropertySource relative to jar fileJava Spring 参考 PropertySource 相对于 jar 文件
【发布时间】:2017-03-01 03:18:13
【问题描述】:

我希望能够从应用程序 jar 文件外部配置一些外部配置文件。正在使用@PropertySource 加载配置文件,类似于您在下面看到的内容;

@PropertySource("classpath:configuration/myFile.properties)

该应用程序位于与该配置目录相对放置的目录中,见下文。

\Workingdirectory
--\configuration
+++myfile.properties
--myApp.jar

如何设置类路径以期望这个相对放置的目录?

【问题讨论】:

  • 一般规则是那些运行时覆盖应该在命令行参数或环境变量中。

标签: java spring gradle jar


【解决方案1】:

您可以使用“文件:”模式来关联外部资源

PropertySource("file:${app.home}/app.properties")

【讨论】:

  • 谢谢回复,app.home 是从哪里衍生出来的?这是 java spring 特有的吗?
【解决方案2】:

@PropertySource("file:app.properties")

在 maven 项目(没有模块)中,您通常会在其中找到 pom.xml 的项目根目录。

在 maven 项目(具有多个模块)中,主项目的文件夹。如果你有这个例子:

- my-project (located in /home/you/git/)
  - my-webapp
    - src
      + main
      + test
    pom.xml
  + library1
  + library2
  pom.xml
  app.properties <-- this is used

 localhost[/home/you/git/my-project]# mvn tomcat:run

@PropertySource("app.properties")

在 maven 项目中(没有模块)是指当前类加载器的类路径(通常位于 src/main/resources)。

@PropertySource("file:/app.properties")

在 windows 中表示C:\app.properties

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多