javaspringspring-bootspring-annotationsspring-el

Spring initializing the @Value annotation property from a static field


Having the following code:

public class Constants {
    public static final String X = "-";
}

And in another class

@Value("${some.property:Constants.X")
private String separator;

How can I achieve this? Constant property is not being loaded


Solution

  • You need to use the T operator with the actual package name where your Constants class is located like below

    @Value("#{T(your.package.Constants).X}")
    private String separator;