Java class name as annotation attribute

Is it possible to use a simple class name inside an annotation? My goal is to provide compile-time injection security using JNDI names containing the class.

Example (from what I want to do, this does not execute at compile time):

@EJB(mappedName = "java:module/" + MyService.class.getSimpleName()) private MyService myService; 

class.getSimpleName () seems to be allowed at runtime, which doesn't work with annotations. Can I get it as a constant?

+6
source share
1 answer

Impossible as you have. This can be done using AOP methods. But,

 @EJB(mappedName = "java:module/" + MyService.class.getSimpleName()) private MyService myService; 

In this piece of code you have only one possible value, namely "MyService". It will not change dynamically, so why do you want to evaluate it dynamically?

0
source

Source: https://habr.com/ru/post/952022/


All Articles