0

How to auto convert string values from properties files to other data types i.e. duration using spring conversion service?

properties file

my.duration=PT10M

configuration class

@Value("{my.duration}")
//It'll give string value by default but I want to convert to duration
private Duration myDuration;

How can I achieve that using spring ConversionService?

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
  • Possible duplicate of [Spring Boot bind @Value to Enum case insensitive](https://stackoverflow.com/questions/35565758/spring-boot-bind-value-to-enum-case-insensitive) – Gal Shaboodi Nov 30 '17 at 06:54

1 Answers1

0

Here you can use YAML, if you want to populate a class with the values from resource files.

Duration.yaml

duration:
    val1: "PT"
    val2: "10"
    val3: "M"

Duration.java

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "duration")
public class Duration {
    private String val1;
    private String val2;
    private String val3;

    // setters and getters.
}       

You can play around with this approach to get the desired output.

Avinash
  • 4,115
  • 2
  • 22
  • 41