April 2018
Intermediate to advanced
246 pages
6h 11m
English
Constructor injection can be achieved by using the @Inject annotation at the constructor level. This constructor ought to acknowledge class dependencies as arguments. Multiple constructors will, at that point, assign the arguments to their final fields:
public class AppConsumer { private NotificationService notificationService; //Constructor level Injection @Inject public AppConsumer(NotificationService service){ this.notificationService=service; } public boolean sendNotification(String message, String recipient){ //Business logic return notificationService.sendNotification(message, recipient); }}
If our class does not have a constructor with @Inject, then it will be considered a default constructor with no arguments. ...