- Developing Middleware in Java EE 8
- Abdalla Mahmoud
- 455字
- 2021-07-23 19:24:33
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!
- Flask Web全棧開發(fā)實(shí)戰(zhàn)
- AngularJS入門與進(jìn)階
- Node.js Design Patterns
- WSO2 Developer’s Guide
- 基于差分進(jìn)化的優(yōu)化方法及應(yīng)用
- Mastering Rust
- Elasticsearch Server(Third Edition)
- Getting Started with Nano Server
- 算法設(shè)計(jì)與分析:基于C++編程語言的描述
- 精通Spring:Java Web開發(fā)與Spring Boot高級功能
- 3ds Max 2018從入門到精通
- Laravel Design Patterns and Best Practices
- 從零開始學(xué)UI設(shè)計(jì)·基礎(chǔ)篇
- 用Go語言自制編譯器
- Java Web應(yīng)用開發(fā)