Praneeth
Praneeth

Reputation: 769

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);
        }
    
    }

Upvotes: 16

Views: 9109

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

Use the T operator:

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

but make the constant public.

Upvotes: 25

Related Questions