Search code examples
javaspringspring-el

Accessing static variable in spring annotations using spel


I have a value in annotation, for which I want to assign a static variable I tried something like this

@Cacheable(value = "#com.test.App.VALUE")
public List someCachableMethod() { }

After trying this its still same Exception : field or Property cannot be found or null

public class App {
    private static String MY_NAME = " XXXX";
    public static void main(String[] args) {
            ExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("#MY_NAME)");
            String message = (String) exp.getValue();
            System.out.println("---------------->"+message);
        }
    
    }

Solution

  • Use the T operator:

    "#{T(com.test.App).VALUE}"
    

    but make the constant public.