I am trying to learn Dagger2 but module is coming as deprecated. I am following tutorial from http://www.vogella.com/tutorials/Dagger/article.html Here is my code as follow.
public class TestApp {
@Inject
BackendService backendService;
private void testServer() {
BackendComponent backendComponent = DaggerBackendComponent.builder()
.build();
backendComponent.inject(this);
backendService.call();
}
public static void main(String[] args) {
new TestApp().testServer();
}
}
UserModule whose injection is not working
@Module
public class UserModule {
@Provides
@Singleton
User providesUser() {
return new User("John", "Doe");
}
}
BackendService
public class BackendService {
@Inject
public User user;
private String serverUrl;
@Inject
public BackendService(@Named("serverUrl") String serverUrl) {
this.serverUrl = serverUrl;
}
public boolean call() {
if (user != null && serverUrl != null && serverUrl.length() > 0) {
System.out.println("User: " + user.toString());
return true;
} else {
System.err.println("User: " + user);
System.err.println("ServerUrl: " + serverUrl);
return false;
}
}
}
BackendServiceModule
@Module
public class BackendServiceModule {
@Provides
@Singleton
BackendService proviedBackendServiceModule(@Named("serverUrl") String serverUrl) {
return new BackendService(serverUrl);
}
@Provides
@Named("testUrl")
String provideAnotherUrl() {
return "http://www.facebook.com";
}
@Provides
@Named("serverUrl")
String provideServerUrl() {
return "http://www.google.com";
}
}
BackendComponent
@Singleton
@Component(modules = {UserModule.class, BackendServiceModule.class})
public interface BackendComponent {
BackendService proviedBackendServiceModule();
void inject(TestApp app);
}
I am getting output as follows after running:
User: null
ServerUrl: http://www.google.com
If i try following code its working
public class TestApp {
@Inject
User user;
@Inject
BackendService backendService;
private void testServer() {
BackendComponent backendComponent = DaggerBackendComponent.builder()
.build();
backendComponent.inject(this);
backendService.call();
System.out.println("User: " + user);
}
public static void main(String[] args) {
new TestApp().testServer();
}
}
User: null
ServerUrl: http://www.google.com
User: User [firstName=John, lastName=Doe]
The reason for the your problem is the way dagger works. It only injects fields annotated with @Inject on the class you specified as parameter of the inject method. Therefore nothing inside your BackendService annotated with @Inject will be injected.
public class TestApp {
@Inject <-- this is injected
User user;
@Inject <-- this is injected
BackendService backendService; <-- Nothing inside this class will be injected
I would recommend to pass your user object to your BackendService inside the constructor. Just do something like the this:
@Provides
@Singleton
BackendService proviedBackendServiceModule(@Named("serverUrl") String serverUrl, User user) {
return new BackendService(serverUrl, user);
}
Another solution would be to define an inject method for the BackendService inside your component, but it will get messy passing around the component object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.