Inject

Guice provides the @Inject annotation to indicate that a consumer is dependent on a particular dependency. The injector takes care of initializing this dependency using the object graph. Guice gets a hint from the annotation that it needs to participate in the class construction phase. The @Inject annotation can be utilized in a constructor for a class, for a method, or for a field. Consider the following code:

//Field level injection
@Inject
private NotificationService notificationService;

//Constructor level Injection
@Inject
public AppConsumer(NotificationService service){
this.notificationService=service;
}

//Method level injection
@Inject
public void setService(NotificationService service) {
this.notificationService = service;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.163.13