java - 无法使用 configurationproperties 注释读取属性
问题描述
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
GlobalProperties globalProperties = context.getBean(GlobalProperties.class);
System.out.println(globalProperties);
}
}
@Component
@PropertySource("classpath:global.yml")
@ConfigurationProperties("app")
public class GlobalProperties {
private String error;
private List<Menu> menus = new ArrayList<>();
private Compiler compiler = new Compiler();
public static class Menu {
private String name;
private String path;
private String title;
//getters and setters and tostring
}
public static class Compiler {
private String timeout;
private String outputFolder;
//getters and setters and tostring
}
@Override
public String toString() {
return "GlobalProperties [error=" + error + ", menus=" + menus + ", compiler=" + compiler + "]";
}
}
src/main/resources 文件夹中的 global.yml 文件
logging:
level:
org.springframework.web: ERROR
com.mkyong: DEBUG
email: test@mkyong.com
thread-pool: 10
app:
menus:
- title: Home
name: Home
path: /
- title: Login
name: Login
path: /login
compiler:
timeout: 5
output-folder: /temp/
error: /error/
当我运行应用程序时,我没有看到 global.yml 属性被正确读取。我把它当作 o/p
GlobalProperties [error=null, menus=[], compiler=Compiler{timeout='null', outputFolder='null'}]
我是否错过了上述代码中的任何内容以从 yml 文件中读取属性。我也尝试用 global.properties 替换 global.yml ,但这也没有用。
解决方案
您必须将设置器添加到您的 GlobalProperties 类,就像您为嵌套静态类所做的那样。
推荐阅读
- compiler-errors - Gnuplot - 误差线
- java - 引起:java.lang.NumberFormatException:对于输入字符串:“?” 对于 cron 工作
- django - 使用 pk 以外的属性识别 Django Rest Framework ModelSerializer ForeignKey
- c# - Visual Studio - 调试时以黄色突出显示的行
- python - WSAECONNREFUSED:通过 PyRFC 建立连接时连接被拒绝
- go - 如何处理响应 JSON 具有没有键的自定义字段?
- android - java.lang.IllegalStateException:全局数据库持有者未初始化。确保在访问数据库之前调用 FlowManager.init()?
- kubernetes - Kubernetes 在不同网络中的设置
- .net - Vb.net在面板内的自定义文本上添加占位符
- vue.js - 在 vuetify 中添加 v-tooltip 到 v-treeview