[英]Getting null values while reading application.yaml file in java
I am trying to fetch the data from my application.yaml file which contain profile details, into variables我正在尝试将包含配置文件详细信息的 application.yaml 文件中的数据提取到变量中
application.yaml file contents application.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应用程序.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?你能帮我理解为什么它没有从 yaml 文件中选择值吗?
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):在您的 yml 中,您所拥有的内容被格式化为“.properties”文件格式,您需要将您的属性格式化为 yml 格式。因此,您的 yml 文件应该采用以下格式(类似这样):
spring:
profiles:
active: dev
config:
activate:
onProfile: dev
application:
id : dev-app
etc... ETC...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.