Purpose of @Nonbinding and annotation-valued qualifiers
by Scott Stark
Both the CDI 1.0 and 1.1 draft include this comment about Nonbinding:
Array-valued or annotation-valued members of a qualifier type should be annotated @Nonbinding in a portable application.
If an array-valued or annotation-valued member of a qualifier type is not annotated @Nonbinding , non-portable behavior
results.
What is the non-portable behavior one might see with annotation-valued qualifiers without @Nonbinding?
I'm finding the usage of qualifiers with annotation member values difficult to deal with when using @Nonbinding as I posted in a previous thread. The canonical usecase of the HttpParam from "Chapter 4. Dependency injection and programmatic lookup" of the Weld user docs I would argue should be written like this:
@BindingType
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface HttpParam {
public String value();
}
class HttpParams
@Produces @Nonbinding @HttpParam("")
String getParamValue(InjectionPoint ip) {
ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value());
}
}
So that only the producer method has the @Nonbinding annotation. The meaning of the @Nonbinding annotation on the producer method is that all qualifier annotation values are ignored when matching the method for selection.
This would allow an HttpParam user to differentiate both field injection as well as string events:
class SomeHttpParamUser {
@HttpParam("username") String username;
@HttpParam("password") String password;
public void setUsername(@Observes @HttpParam("username") String username) {...}
public void setPassword(@Observes @HttpParam("password") String username) {...}
}
Right now to get the same behavior one has to use an HttpParamNonbinding annotation that would include the @Nonbinding on the value on the fields and another HttpParam without the @Nonbinding on the event observer methods.
12 years, 4 months