javax.inject
Annotation Type Qualifier


@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Qualifier

Custom binding annotations are marked with @Qualifier as a meta-annotation.

Example: creating a custom binding type

 package example;

 import static java.lang.annotation.ElementType.*;
 import static java.lang.annotation.RetentionPolicy.Runtime;
 import java.lang.annotation.*;

 import javax.inject.Qualifier;

 @Qualifier
 @Documented
 Target({TYPE, METHOD, FIELD, PARAMETER})
 Retention(RUNTIME)
 public @interface MyBinding {
 }
 

Example: injecting a servlet using a custom binding type

 package example;

 import example.MyBinding;
 import javax.servlet.*;
 import java.io.*;

 public class MyServlet extends GenericServlet {
   @MyBinding MyBean _bean;

   public void service(ServletRequest req, ServletResponse res)
     throws IOException
   {
     PrintWriter out = res.getWriter();

     out.println("my-bean: " + _bean);
   }
 }
 

Example: configuring using a custom qualifier

META-INF/beans.xml
 <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:example="urn:java:example">

   <example:MyBean>
     <example:MyQualifier/>
   </example:MyBean>

 </beans>