读取应用程序时获取空值.java中的Yaml文件



我正试图从我的应用程序中获取数据。包含概要信息的Yaml文件,放入变量

应用程序。文件内容

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);
}
}

输出:

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

你能帮我理解为什么它不从yaml文件中选择值吗?

在您的html中,您所拥有的是格式化在"。属性的文件格式,您需要将属性格式化为yml格式。因此,您的yml文件的格式应该是这样的(类似于):

spring:
profiles:
active: dev
config:
activate:
onProfile: dev
application:
id : dev-app

等等……

最新更新