We are using Guice for dependency injection. I have a parent class where I need a dependency and that parent class calls other class methods where EVERY sub-class method needs the same dependency. My idea is that I should use the @Inject annotation only once one the parent class to inject that dependency and once we get that dependency we should pass this as arguments to the other sub-class methods. Is this a correct approach? I dont think we need follow the approach of calling @Inject on the sub classes separately to avoid parameter passing. The approach would only make sense in my opinion if we are passing the dependency several layers down the state and some of the intermediate layers donot need that dependency. Please correct me if I am wrong.
Sample code for illustration==
public class SDBResource {
private final AbstractAuthenticationDetailsProvider authDetailsProvider;
private final SDBService service;
@Inject
//pass detailsProvider parameter directly to doSomething method instead of doing another
// @Inject in SDBService
public SDBResource(AbstractAuthenticationDetailsProvider detailsProvider, SDBService service) {
this.authDetailsProvider = detailsProvider;
service.doSomeThing(detailsProvider);
}
}
//No need to inject detailsProvider here
public class SDBService {
public void doSomeThing(AbstractAuthenticationDetailsProvider detailsProvider) {
// do something
}
}