Search posts, tags, users, and pages
JS Rammohan
First of all, thanks for the detailed explanation about solid principles usage. I have a question about how to initialize runtime object of PaypalPaymentService for Rest Controller in DIP example.
Amr Khaled
Thank you so much, and sorry for the very late reply.
You can initialize it in multiple ways:
1- Constructor injection:
private final PaymentService creditCardPaymentService; private final PaymentService payPalPaymentService;
@Autowired public PaymentController(CreditCardPaymentService creditCardPaymentService, PayPalPaymentService payPalPaymentService) { this.creditCardPaymentService = creditCardPaymentService; this.payPalPaymentService = payPalPaymentService; }
2- With qualifiers:
@Qualifier("payPalPaymentService") public class PayPalPaymentService extends PaymentService {
and then in your controller or other service:
@Autowired public PaymentController(@Qualifier("creditCardPaymentService") PaymentService creditCardPaymentService, @Qualifier("payPalPaymentService") PaymentService payPalPaymentService) { this.creditCardPaymentService = creditCardPaymentService; this.payPalPaymentService = payPalPaymentService; }
An Enthusiastic Software Engineer