在读取application.yaml文件时获取null值

发布于 2025-02-06 05:44:24 字数 1070 浏览 1 评论 0原文

我正在尝试从我的应用程序中获取数据。包含配置文件详细信息的yAML文件,in variables

文件

spring.profiles.active: dev
---
spring.config.activate.on-profile: dev

application.id : dev-app
my.server : localhost:8080
---
spring.config.activate.on-profile: uat

application.id : uat-app
my.server : localhost:8081

@SpringBootApplication
public class App {

@Value("${application.id}")
private String applicationId;

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

 public static void main(String args[]) {

    SpringApplication.run(App.class, args);

    App app = new App();
    app.display();

}

public void display(){
   System.out.println("Application Id : "+ applicationId);
   System.out.println("Server : "+ server);
}

}

目录

2022-06-08 19:38:29 main INFO  App:640 - The following 1 profile is active: "dev"
2022-06-08 19:38:30 main INFO  App:61 - Started App in 2.266 seconds (JVM running for 3.345)
Application Id : null
Server : null

application.yaml

I am trying to fetch the data from my application.yaml file which contain profile details, into variables

application.yaml file contents

spring.profiles.active: dev
---
spring.config.activate.on-profile: dev

application.id : dev-app
my.server : localhost:8080
---
spring.config.activate.on-profile: uat

application.id : uat-app
my.server : localhost:8081

App.java

@SpringBootApplication
public class App {

@Value("${application.id}")
private String applicationId;

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

 public static void main(String args[]) {

    SpringApplication.run(App.class, args);

    App app = new App();
    app.display();

}

public void display(){
   System.out.println("Application Id : "+ applicationId);
   System.out.println("Server : "+ server);
}

}

Output:

2022-06-08 19:38:29 main INFO  App:640 - The following 1 profile is active: "dev"
2022-06-08 19:38:30 main INFO  App:61 - Started App in 2.266 seconds (JVM running for 3.345)
Application Id : null
Server : null

Could you please help me to understand why it is not picking the values from yaml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-02-13 05:44:24

文件格式,您需要将属性格式化为YML格式。

spring:
  profiles:
    active: dev
  config:
    activate:
  onProfile: dev

application:
  id : dev-app

在YML中,您所拥有的格式是“ .properties的

in your yml, what you have is formatted in a ".properties' file format, you need to format your property to yml format. So, your yml file should be in the format (something like this):

spring:
  profiles:
    active: dev
  config:
    activate:
  onProfile: dev

application:
  id : dev-app

etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文