在 this文章中我们可以看到如何在 application.properties
中生成随机属性。是否还有一些方法可以在 application.properties
中使用 java 代码生成一些其他属性,例如当前年份?
我尝试过这个:
config.current-year=${LocalDate.now.getYear}
但它抛出异常:
Caused by: java.lang.NumberFormatException: For input string: "${LocalDate.now.getYear}"
更新:
在 iamrajshah 回答之后,我看到随机表示类 RandomValuePropertySource
是以这种方式加载的:
protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment);
new Loader(environment, resourceLoader).load();
}
所以我需要的只是覆盖(现在我不知道)这个函数并在那里添加我可以使用的类。
最佳答案
You can check this out
application.properties
就像之前版本的spring中的*.xml
注册类似。
Spring boot提供了RandomValuePropertySource
用于生成值。
此文件中不支持任何其他类。
关于java - 如何在 Spring Boot 属性中生成当前年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54002466/