Languages
[Edit]
EN

Spring Boot 2 - convert duration value in application.properties to java.time.Duration object

8 points
Created by:
Jasmin
305

In this short article, we would like to show how to convert duration property value located in application.properties to java.time.Duration object when @Value annotation in Spring Boot Application is used.

Quick solution:

1. in application.properties file:

...

spring.web.resources.cache.period=P30D

...

Note: it is important to use P suffixed format for values.

2. in some component:

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Controller;

import java.time.Duration;

@Controller
public class ExampleController
{
    @Value("#{T(java.time.Duration).parse('${spring.web.resources.cache.period}')}")
    private Duration cachePeriod;

    // ...
}

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join