- Developing Middleware in Java EE 8
- Abdalla Mahmoud
- 449字
- 2021-07-23 19:24:34
Using producers
As shown earlier, a bean can have different alternatives, by introducing one interface and providing different implementations, each with a different qualifier. When injecting a reference to this interface in another bean, you can annotate your injection point with the qualifier for the implementation you desire. One interesting question is, can we specify which implementation to inject according to some runtime parameters, such as a user-specified choice?
For example, suppose a user is engaged in a payment workflow process. The first step is that the user will choose which payment method they prefer and where the next step they will actually perform the payment transaction. Suppose you have a PaymentStrategy interface with different bean implementations for a credit card, PayPal, and check payment strategies. Can we specify which bean implementation to reference according to the user choice? The answer is yes! This is called runtime polymorphism, and it can be achieved using producers.
A producer is a method that acts as a source of CDI beans. Consider the following example for a bean with different implementations:
public interface PaymentStrategy { ... } public class CreditCardPaymentStrategy implements PaymentStrategy{ ... } public class CheckPaymentStrategy implements PaymentStrategy{ ... } public class PayPalPaymentStrategy implements PaymentStrategy{ ... }
Let's define a Preferences bean. The preferences bean will act as a representation for the user-chosen preferences, and will include a producer method as follows:
@SessionScoped public class Preferences implements Serializable { public static final int CREDIT_CARD = 0; public static final int CHECK = 1; public static final int PAYPAL = 2; private int paymentStrategy = ...; @Produces @Preferred public PaymentStrategy getPaymentStrategy() { switch (paymentStrategy) { case CREDIT_CARD: return new CreditCardPaymentStrategy(); case CHECK: return new CheckPaymentStrategy(); case PAYPAL: return new PayPalPaymentStrategy(); default: return null; } } }
The getPaymentStrategy method is annotated with a @Produces annotation and this is called a producer method. It returns an instance for a PaymentStrategy implementation. The @Preferred annotation is a qualifier that will be used when injecting a reference to our bean, as follows:
@Qualifier @Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER}) public @interface Preferred { }
Now, assume you have injected a reference to a PaymentStrategy as follows:
@Inject @Preferred PaymentStrategy paymentStrategy;
The method will run and detect which choice was made by the user for their preferred payment strategy, and, will return the appropriate implementation. The producer method technique is really useful when you need to choose one implementation in runtime according to a given parameter. However, there are other cases when using the producer method is useful, such as:
- You need to inject objects that are not real CDI beans
- You need to satisfy some initial values for your bean and/or performing some initial operations
- 微信公眾平臺與小程序開發(fā):從零搭建整套系統(tǒng)
- JavaFX Essentials
- Implementing Cisco Networking Solutions
- JSP開發(fā)案例教程
- Python面向對象編程:構建游戲和GUI
- Unity Game Development Scripting
- Learning JavaScript Data Structures and Algorithms
- D3.js By Example
- 用案例學Java Web整合開發(fā)
- Solutions Architect's Handbook
- Web App Testing Using Knockout.JS
- Python計算機視覺和自然語言處理
- MongoDB Cookbook(Second Edition)
- 算法超簡單:趣味游戲帶你輕松入門與實踐
- WCF編程(第2版)