java - 如何在 Spring Boot 属性中生成当前年份

标签 java spring-boot

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/

相关文章:

java - 如何从 JFrame 屏幕截图中删除标题栏?

java - Spring Boot fat jar 中出现 NoClassDefFoundError 但依赖项位于已编译的存档中,并且应用程序在 IDE 中运行良好

java - 如何为 GET API 编写 JUnit 测试用例?

spring - 如何仅打包 `distTar`这样的dockerfile + jar不能构建镜像

javascript - Spring Boot - 为什么我的应用程序找不到静态资源?

java - 后有 2 位数字的结果。例如 :23. 65

java - 关于 Java 中的键绑定(bind)

java - 将对象数组从 Java 返回到 C++

java - 由于上下文问题无法执行单元测试?

java - 从不规则的二维字符串数组中删除字符串并在此过程中缩短数组