What is the purpose of @Nonbinding annotations in Qualifier, which should be in Java EE7?

I read through CDI injections in JavaEE 7, in particular using @Qualifier and @Produces to introduce custom Data type in a bean.

I have the following code taken from the JBoss documentation at the end 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 can be used as follows:

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

My question is:

  • What does the @Nonbinding annotation @Nonbinding ? and why is it needed?

  • If the method signature should always be like this @Nonbindng public String value(); . The reason I'm asking about this is a few examples, but they all have the same signature. This is valid:

  public @interface HttpParam {
        @Nonbinding public int value ();
     }
  1. May I have several methods defined in the interface. That is, is it permissible or not?
  public @interface HttpParam {
        @Nonbinding public String value ();
        @Nonbinding public int value1 ();
     } 

thanks

+6
source share
1 answer
  • By default, classifier arguments are considered to match bean qualifiers with injection point qualifiers. The @Nonbinding argument @Nonbinding not considered for matching.

  • In this case, the bean created by the producer method has the @HttpParam("") qualifier. If the argument was required (i.e. Not @Nonbinding ), @HttpParam("") would not match @HttpParam("username") at the injection point.

  • You can have any number of qualifier arguments, binding, or optional.

In the CDI specification, see Type Resolution .

+11
source

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


All Articles