Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

Turn a Java annotation into a CDI qualifier

04.24.2011
| 4737 views |
  • submit to reddit

A CDI qualifier is an annotation that itself is annotated with the @javax.inject.Qualifier meta-annotation. Per example, if you add the @Qualifier to the MyAnnotation (http://e-blog-java.blogspot.com/2011/04/what-is-java-annotation.html) you obtain a CDI qualifier:

@Qualifier
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
// Property Definitions here.
}

CDI provide a set of built-in qualifiers:

Qualifier                         
 
 
Description
 
@javax.enterprise.inject.Named 
 
This qualifier is used to un-typed access from
non-Java code. Commonly this qualifier serves the
JSF pages that access beans through EL.
 
@javax.enterprise.inject.New 
 
This qualifier forces the creation of a new
instance, instead of using the contextual
instance. It allows us to obtain a dependent
object of a specified class.
 

@javax.enterprise.inject.Any
 
This qualifier “belongs” to all beans and 
injection points (not applicable when @New is
present). This is useful if you want to iterate
over all beans with a certain bean type.
 
@javax.enterprise.inject.Default
 
As the qualifier name suggests, whenever a bean
or injection point does not explicitly declare a
qualifier, the container assumes the qualifier
@Default.

 

From http://e-blog-java.blogspot.com/2011/04/turn-java-annotation-into-cdi-qualifier.html

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Wujek Srujek replied on Mon, 2011/04/25 - 8:46am

Instead of that, you can add a so called 'CDI portable extension', listen to the BeforeBeanDiscovery and use its addQualifier method. The annotation you add doesn't have to be declared a @Qualifier, actually, it shouldn't be as such annotations are discovered automatically (as long as in a bean archive). This way you can keep your annotation clean of the CDI-specific meta-annotations. Tested with Weld.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.