开发者

Dynamically passing key to @Named annotation

开发者 https://www.devze.com 2023-04-05 05:57 出处:网络
I\'m facing the following issue. Could any one help understand why is it so? Also please share your thoughts on making the 2nd scenario work.

I'm facing the following issue. Could any one help understand why is it so? Also please share your thoughts on making the 2nd scenario work.

  1. This works without any compilation issues,

    private final String var ="US";
    @Inject @Named (var) private Validator validator; 
    
  2. This is giving compilation errors, Compilation Error:"The value for annotation attribute Named.value must be a constant开发者_StackOverflow expression"

    private final String var =System.getProperty("US");
    @Inject @Named (var) private Validator validator;
    


In the first case the compiler sees that a constant has been assigned to a private final variable, so it optimizes away the variable and uses the constant in its place. In the second case it can't do that, so it's left with a variable in the annotation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号