user1828324
user1828324

Reputation: 31

Accessing spring annotation attribute in method

This might be a strange request but is there a way to access Spring annotation attribute in the method itself.

In the example below one can define "user.success" as a constant and use it in the Annotation and the method but I would really like use whats defined in my annotation(@MySecurity).

One way I guess would be get a handle of the executing method to use AnnotationUtils. Is there a better more elegant way of doing this?

 @MySecurity(action="user.success")
 @RequestMapping("/userSuccess.htm")
 public String redirect()
 {
    // Code here to access @MySecurity Annotation
    return "userSuccess";
 }

Upvotes: 0

Views: 138

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279920

No, it is not possible without reflection. And even then, you need to know the name of the method. Executing code in it doesn't tell you that (except stack trace but don't go that path). Honestly, the annotation should be used by a HandlerInterceptor (with HandlerMethod where you would have access to the annotation) or some kind of BeanPostProcessor.

Upvotes: 1

Related Questions