Spring boot 2 Converting Duration java 8 application.properties

后端 未结 5 1719
抹茶落季
抹茶落季 2021-02-20 08:48

I need to define Duration value (spring.redis.timeout) by application.properties.

I was trying to use one point defined in Spri

5条回答
  •  面向向阳花
    2021-02-20 09:14

    Spring Boot attempts to coerce the external application properties to the right type when it binds to the @ConfigurationProperties beans. If you need custom type conversion, you can provide a ConversionService bean (with a bean named conversionService)

    See: https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#boot-features-external-config-conversion

    Create new ApplicationConversionService bean (it must be named conversionService ). Here you are my code tested with Spring boot 2.0.4:

    @Configuration
    public class Conversion {
    
    @Bean
    public ApplicationConversionService conversionService()
    {
        final ApplicationConversionService applicationConversionService = new ApplicationConversionService();
        return applicationConversionService;
    }
    

    Here you are an example project using this approach:

    https://github.com/cristianprofile/spring-data-redis-lettuce

提交回复
热议问题