Passing and using properties through annotations in Java
https://softwareengineering.stackexchange.com/questions/394544
-
27-02-2021 - |
Question
I have a Java EE Application in which I have an Interceptor class like this
@Interceptor
@Logged
public class LogInterceptor {
@AroundInvoke
public Object logMethod(InvocationContext context) throws Exception {
...
}
}
Now when I want it to work I annotate the target method with @Logged
. Please correct me if my perception of this is wrong.
Now to the question.
Is it possible to pass/bind a variable (and when, how?) throught the annotation like so:
@Logged(ctx = methodContext)
public void someMethod(MethodContext methodContext) {
...
}
and then a use it in LogInterceptor.logMethod()
?
Or are there other options to accomplish this?
No correct solution
OTHER TIPS
The values of annotation parameters must be compile-time constants, so you cannot "pass a variable" this way. But how should this even work? Where would methodContext be defined and who would be responsible for setting the value? You are, however, free to pass compile-time constants, so for example:
@Logged(loggerName = "MY_LOGGER")
public void ...
is completely fine.