11

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

brain storm
  • 30,124
  • 69
  • 225
  • 393

1 Answers1

14
  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.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • 4
    is it possible to give an example with Binding and Non-binding and show how it differs. That will be very helpful please – brain storm Oct 08 '14 at 21:13
  • I was experiencing the exact behavior you have described in 2nd explanation when trying to use Interceptor binding with parameterized Annotations. I was desperate to find a solution to it. You saved my day. Thanks a lot. – TMtech Jun 22 '18 at 06:35