【问题标题】:yml configuration file with spring framework带有spring框架的yml配置文件
【发布时间】:2016-06-12 16:32:51
【问题描述】:

我正在使用具有 yml 配置文件的 spring 框架开发 jhipster 项目,我需要从资源/配置路径中的 application.yml 文件中获取 url 和 serverport 参数,我有以下代码,但它打印为 null。

configuration.yml 文件

server:
     port: 8081

spring:
    devtools:
       restart:
         enabled: true
    datasource:
       url: jdbc:postgresql://localhost:5432/adap_ms

java类

  @Configuration
  public class Configuration {

  @Value("${spring.datasource.url}")
  private String url;

  @Value("${server.port}")
  private String serverPort;

  public void generateReport() throws Exception{ 
     System.out.println(url);
     System.out.println(serverPort);
 }

【问题讨论】:

  • 尝试将文件重命名为resources/config/application.yml

标签: java spring-boot yaml jhipster


【解决方案1】:

我认为你的资源目录应该是resources/config。 Spring boot 将类路径中的 config 目录下的文件以及运行应用程序的当前目录视为配置文件。检查第 24.3 节中的documentation。

根据 Joost 的评论编辑。是的,您可能需要将 configuration.yml 文件名更改为 application.yml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2018-06-28
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多