在读取application.yaml文件时获取null值
我正在尝试从我的应用程序中获取数据。包含配置文件详细信息的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件格式,您需要将属性格式化为YML格式。
在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):
etc...