【问题标题】:Spring Boot passing value / using method inside the application.propertiesSpring Boot 在 application.properties 中传递值/使用方法
【发布时间】:2019-05-30 15:00:23
【问题描述】:

例如:

我有一个名为 Path.java 的类

在 Path.java 中,我有返回 int 8080 的 getter 方法“getPort()”

在 application.properties 中,我想使用 server.port = "getPort()"

怎么做?

我想将数据源外部化。我已经有一个从外部 .txt 文件中获取信息的类。我需要在 application.properties 中使用这个类。

比如 spring.datasource.url = ??getURL()??

【问题讨论】:

  • 属性不能这样工作。它们的目的是收集为应用程序提供的静态内容作为配置。
  • 答案是否定的,可以在现有的spring boot属性文件中这样实现。
  • 最好的方法是作为运行时变量传递
  • 有一些方法,但它们比在启动前提取信息(通常在shell脚本中)和设置环境变量PORT的直接方法要复杂得多。

标签: java spring-boot application.properties


【解决方案1】:

思考如何在 Path.java 中获得 port。然后只需重用该信息。无法调用属性文件中的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 2019-03-18
    • 2018-01-07
    • 2015-10-27
    • 2014-11-30
    • 2018-02-25
    • 2014-11-04
    • 2018-09-15
    相关资源
    最近更新 更多