A qualifier is an annotation that has @Qualifier
declared on it, which signifies to the container that this qualifier annotation can be utilized on injection points and beans to distinguish different implementations of the same bean type.
A qualifier annotation, without any members, is just:
@Qualifier @Retention(RUNTIME) @Target( { TYPE, METHOD, FIELD, PARAMETER } ) public @interface MyQualifier {}
We can use field injection with @MyQualifier
by:
@Inject @MyQualifier private Locale myLocale;
Or constructor injection:
@Inject public Notifications(@MyQualifier Locale myLocale) { this.myLocale = myLocale; }
Or an initializer method:
@Inject public void setLocale(@MyQualifier Locale myLocale { this.myLocale = myLocale; }
Lastly, producing a Locale
with the @MyQualifier
annotation to match the injection points:
@Produces @MyQualifier public Locale getMyLocale() { return Locale.US; }