An injection point is identified by the @Inject
annotation. Previously, we covered a nondefault constructor for a bean that was annotated with @Inject
, as shown in the following code:
public class PaymentProcessor { private final Payment payment; @Inject public PaymentProcessor(Payment payment) { this.payment = payment; } }
This is known as bean constructor parameter injection and there can only be one constructor annotated with @Inject
in a bean.
If a single constructor that defines every bean that we need to use, and thus needs to be injected, is not favored, there are two other ways to inject into our bean:
Create a bean that utilizes initializer method parameter injection, which has no restrictions on how many methods may be annotated with
@Inject
. If we were to change thePaymentProcessor
class to use initializer method parameter injection, it would look like the following code snippet:public class PaymentProcessor { private final Payment payment...