I have a configuration parameter "myconfig.defaultSize" whose value is defined, for example, as "10MB", in the application.properties file.
In the other hand, I have a @Component class with @ConfigurationProperties annotation mapping those configuration parameter, as follows.
@Component
@ConfigurationProperties(prefix="myconfig")
public class StorageServiceProperties {
private Long defaultSize;
//...getters and setters
}
So, how can I would apply a method to convert the String value into Long?
You can't have such generic converter applied on a property-to-property basis. You could register a converter from String to Long but it would be called for every such case (any property of type Long basically).
The purpose of @ConfigurationProperties
is to map the Environment
to a higher-level data structure. Perhaps you could do that there?
@ConfigurationProperties(prefix="myconfig")
public class StorageServiceProperties {
private String defaultSize;
// getters and setters
public Long determineDefaultSizeInBytes() {
// parsing logic
}
}
If you look at the multipart support in Spring Boot, we keep the String value and we use the @ConfigurationProperties
object to create a MultipartConfigElement
that is responsible of the parsing. That way you can specify those special values in code and configuration.