简体   繁体   English

如何通过 application.properties 注入日期值

[英]How to inject Date values by application.properties

Is there a way to inject Date values by application.properties in Spring-Boot projects.有没有办法在 Spring-Boot 项目中通过 application.properties 注入日期值。 Like this.像这样。

@Component
@ConfigurationProperties(prefix = "foo")
public Class FooConfiguration {
    private Date startTime;
    //getter and setter
}
foo.startTime="2019-03-18 00:00:00"

You can configure a custom converter for configuration properties class like follow:您可以为配置属性类配置自定义转换器,如下所示:

DateConverter.java日期转换器.java

@Component
@ConfigurationPropertiesBinding
public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        if (source == null) {
            return null;
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
    }
}

application.properties应用程序属性

foo.start-time=2019-03-18 00:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何注入按前缀分组的 Spring application.properties? - How to inject Spring application.properties grouped by prefix? 如何在 Spring 中更改 Application.properties 的值 - how to change values of Application.properties in Spring 如何在构造函数中访问application.properties值 - How to access application.properties values in constructor @Value不能从application.properties插入数据 - @Value not inject data from application.properties 在 GitLab CICD 中注入 application.properties 值 - Inject application.properties value in GitLab CICD 如何使用Spring Boot在application.properties中注入Maven配置文件的属性 - how to inject maven profile's properties in application.properties with spring boot 如何在 JAVA 的 application.properties 文件中动态更改值 - How to change values dynamically in application.properties file in JAVA 如何在 javax.validation 注释中使用 application.properties 值 - How to use application.properties values in javax.validation annotations Spring如何在运行时从application.properties重新加载值 - Spring how to reload the values from application.properties at runtime Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM