官术网_书友最值得收藏!

Using qualifiers

A qualifier is a user-defined annotation that is used to tell the container which version of the bean implementation we wish to use at runtime. The idea of qualifiers is too simple; we define a qualifier, and then we annotate both the bean and injection point with this qualifier.

Let's define our first qualifier for the newly created bean implementation and create a new annotation with the following code:

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

As you see, the qualifier is a custom-defined annotation, which itself is annotated with the @Qualifier annotation. @Qualifier tells the container that this annotation will act as a qualifier, while @Retention(RUNTIME) tells the JVM that this annotation should be available for reflection use at runtime, this way the container can check for this annotation at runtime. @Target{TYPE, METHOD, FIELD, PARAMETER} tells the compiler that this annotation can be used on types, methods, fields, and parameters. Anyway, the @Qualifier annotation is the key annotation here.

I have summarized the annotations that we have just mentioned in this table, to make it clearer:

 

Now, let's go to the next step; we will add the @AnotherImp annotation to AnotherPojoImp as follows:

@Dependent 
@AnotherImp 
public class AnotherPojoImp implements MyPojo{ 
 
    @Override 
    public String getMessage() { 
        return "Hello from AnotherPojoImp"; 
    } 
} 

The annotation's role here is that it tells the container that this version of the class is called AnotherImp. Now we can reference this version by modifying the servlet as follows:

@WebServlet(urlPatterns = "/cdi-example") 
public class ExampleServlet extends HttpServlet { 
 
    @Inject @AnotherImp 
    private MyPojo myPojo; 
     
    ... 
} 

Run the example, and you should see the following:

Hello from AnotherPojoImp

But how can we reference the original implementation MyPojoImp? There are two options available to do that:

  • Defining another qualifier for MyPojoImp, like the earlier example
  • Using the default qualifier

The default qualifier, as the name suggests, is the default one for any CDI bean that has not been explicitly qualified. Although an explicit declaration for the default qualifier is considered redundant and useless, it's possible to explicitly declare your CDI bean as a default one by using the @Default annotation, as shown in the following revision to the MyPojoImp class:

@Default 
@Dependent 
public class MyPojoImp implements MyPojo{ 
   ... 
} 

Again, @Default is redundant, but you should consider its existence even if you have not explicitly declared it. Now, to reference the MyPojoImp from the servlet, we will rewrite it as follows:

@WebServlet(urlPatterns = "/cdi-example") 
public class ExampleServlet extends HttpServlet { 
 
    @Inject @Default 
    private MyPojo myPojo; 
     
    ... 
} 

This way, the original MyPojoImp implementation will be injected instead. And likewise, we can eliminate the @Default annotation, as the default implementation will be used by default!

主站蜘蛛池模板: 灵台县| 克什克腾旗| 沙洋县| 邳州市| 崇文区| 镇雄县| 虎林市| 景东| 福海县| 清水河县| 桃园县| 东乌珠穆沁旗| 新营市| 通化市| 潍坊市| 辽宁省| 筠连县| 新巴尔虎右旗| 安龙县| 杂多县| 安义县| 平昌县| 宿迁市| 呼和浩特市| 射洪县| 依安县| 华宁县| 茌平县| 鹤峰县| 沁源县| 博罗县| 衡东县| 晋州市| 仁化县| 凌源市| 扬州市| 凭祥市| 沂南县| 乳源| 迁西县| 依安县|