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

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!

主站蜘蛛池模板: 德惠市| 团风县| 临汾市| 梁山县| 双城市| 安达市| 沅陵县| 崇阳县| 宕昌县| 壤塘县| 南靖县| 新河县| 沁阳市| 东乡县| 淳化县| 广平县| 特克斯县| 宜宾县| 革吉县| 调兵山市| 喀喇| 自治县| 东安县| 虹口区| 无锡市| 三明市| 孝义市| 永顺县| 靖边县| 堆龙德庆县| 夹江县| 道真| 雷州市| 虎林市| 龙陵县| 怀仁县| 齐河县| 凤山县| 雅江县| 尉氏县| 新乡市|