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 :
what does
@Nonbindingannotation mean? and why is it needed?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();
}
- 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
By default, qualifier arguments are considered for matching bean qualifiers to injection point qualifiers. A
@Nonbindingargument is not considered for matching.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.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