I am new to CDI with Dagger. I have the following structure. The issue is when I go to fetch backendService in class Main, only the direct backendService is fetched, but the underlying User dependency remains null. Is there anything wrong with this setup.
Class MyComponent
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {UserModule.class, BackEndServiceModule.class})
public interface MyComponent {
User user();
BackendService backendService();
void inject(Main main);
void injectIntoBackendService(BackendService backendService);
}
Class User:
import javax.inject.Inject;
public class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return "User [firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
Class UserModule
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class UserModule {
@Provides @Singleton User providesUser() {
return new User("Lars", "Vogel");
}
}
BackendService
import javax.inject.Inject;
import javax.inject.Named;
public class BackendService {
@Inject public User user;
private String serverUrl;
@Inject
public BackendService(@Named("serverUrl") String serverUrl) {
this.serverUrl = serverUrl;
}
public boolean callServer() {
System.out.println("User: " + user);
if (user !=null && serverUrl!=null && serverUrl.length()>0) {
System.out.println("User: " + user + " ServerUrl: " + serverUrl);
return true;
}
return false;
}
}
BackEndServiceModule
import javax.inject.Named;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class BackEndServiceModule {
@Provides
@Singleton
BackendService provideBackendService(@Named("serverUrl") String serverUrl) {
return new BackendService(serverUrl);
}
@Provides
@Named("serverUrl")
String provideServerUrl() {
return "http://www.vogella.com";
}
@Provides
@Named("anotherUrl")
String provideAnotherUrl() {
return "http://www.google.com";
}
}
Main
import javax.inject.Inject;
public class Main {
@Inject public BackendService backendService;
public void callService() {
boolean callServer = backendService.callServer();
if (callServer) {
System.out.println("Server call was successful. ");
} else {
System.out.println("Server call failed. ");
}
}
public static void main(String[] args) {
Main m = new Main();
MyComponent component = DaggerMyComponent.builder().build();
component.inject(m);
m.callService();
}
}
Please have a look at field injection vs constructor injection. You try to use both partially, ending up to use neither.
When in doubt, use constructor injection. As the name suggests..define dependencies in the constructor and get rid of any other
@Inject
.And when you don't need any further setup you don't need any
@Provides
annotated methods either, so get rid ofprovideBackendService(@Named("serverUrl") String serverUrl)
please. We specified the scope on the implementation class above.I also wrote an article about a few basic things to keep in mind when using Dagger.