what is the purpose of @Nonbinding annotation in a Qualifier supposed to be in Java EE7?

喜你入骨 提交于 2019-12-06 22:34:02

问题


I am reading through the CDI injections in JavaEE 7 particularly using @Qualifier and @Produces to inject a custom Data type into a bean.

I have the following code taken from JBoss documentation towards ends of the page.

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface HttpParam {
   @Nonbinding public String value();
}

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

class HttpParams {
   @Produces @HttpParam("") 
   String getParamValue(InjectionPoint ip) {
      ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
      return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value());
   }
}

And this qualifier could be used in the following way:

@HttpParam("username") @Inject String username;
@HttpParam("password") @Inject String password;

My question is :

  1. what does @Nonbinding annotation mean? and why is it needed?

  2. Should the method signature should always be like this @Nonbindng public String value();. The reason I ask this is I have seen several different examples but they all have the same signature. That is the following allowed:

public @interface HttpParam {
       @Nonbinding public int value();
    }
  1. can I have more than one method defined in the interface. That is, is the following allowed or not?
 public @interface HttpParam {
       @Nonbinding public String value();
       @Nonbinding public int value1();
    } 

Thanks


回答1:


  1. By default, qualifier arguments are considered for matching bean qualifiers to injection point qualifiers. A @Nonbinding argument is not considered for matching.

  2. In this case, the bean produced by the producer method has qualifier @HttpParam(""). If the argument were binding (i.e. not @Nonbinding), @HttpParam("") would not match @HttpParam("username") on the injection point.

  3. You can have any number of qualifier arguments, binding or non-binding.

See Typesafe resolution in the CDI specification.



来源:https://stackoverflow.com/questions/26264323/what-is-the-purpose-of-nonbinding-annotation-in-a-qualifier-supposed-to-be-in-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!