Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 599
brain storm Avatar asked Oct 08 '14 18:10

brain storm


1 Answers

  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.

like image 111
Harald Wellmann Avatar answered Dec 14 '22 23:12

Harald Wellmann