Spring boot 2 Converting Duration java 8 application.properties

后端 未结 5 1721
抹茶落季
抹茶落季 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:03

    The Duration in the moment (Spring-Boot 2.0.4.RELEASE) it is not possible to use together with @Value notation, but it is possible to use with @ConfigurationProperties

    For Redis, you have RedisProperties and you can use the configuration:

    spring.redis.timeout=5s
    

    And:

    @SpringBootApplication
    public class DemoApplication {
    
      @Autowired
      RedisProperties redisProperties;
    
      public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
      }
    
      @PostConstruct
      void init() {
        System.out.println(redisProperties.getTimeout());
      }
    }
    

    It printed (parse as 5s):

    PT5S
    

    https://docs.oracle.com/javase/8/docs/api//java/time/Duration.html#parse-java.lang.CharSequence-

    0 讨论(0)
  • 2021-02-20 09:05

    If your Spring-Boot version or its dependencies don't put ApplicationConversionService into context (and Spring-Boot doesn't until 2.1), you can expose it explicitly

    @Bean
    public ConversionService conversionService() {
        return ApplicationConversionService.getSharedInstance();
    }
    

    It invokes Duration.parse, so you may use PT3S, PT1H30M, etc in properties files.

    0 讨论(0)
  • 2021-02-20 09:11

    Any property which is of type duration can be injected via .properties or .yml files. All you need to do is use a proper formatting.

    If you want to inject a duration of 5 seconds it should be defined as PT5S or pt5s or PT5s

    • case of the letters doesn't matter, so you use any combination which is readable for you
    • generally everyone uses all capital letters

    Other examples

    PT1.5S       = 1.5 Seconds
    PT60S        = 60 Seconds
    PT3M         = 3 Minutes
    PT2H         = 2 Hours
    P3DT5H40M30S = 3Days, 5Hours, 40 Minutes and 30 Seconds
    

    You can also use +ve and -ve signs to denote positive vs negative period of time.

    • You can negate only one of the entity for example: PT-3H30M = -3 hours, +30 minutes, basically -2.5Hours
    • Or You can negate the whole entity: -PT3H30M = -3 hours, -30 minutes, basically -3.5Hours
    • Double negative works here too: -PT-3H+30M = +3 Hours, -30 Minutes, basically +2.5Hours



    Upvote, if it works for you or you like the explanation. Thanks,

    0 讨论(0)
  • 2021-02-20 09:13

    It's possible to use @Value notation with Spring Expression Language

    @Value("#{T(java.time.Duration).parse('${spring.redis.timeout}')}")
    private Duration timeout;
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题