我是Dagger的CDI新手。我有以下结构。问题是当我在Main类中获取backendService时,只获取了直接的backendService,但是底层的User依赖项保持为null。这个设置有什么问题。
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);
}
班级用户:
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 + "]";
}
}
类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";
}
}
主要
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();
}
}
答案 0 :(得分:1)
请查看现场注入与构造函数注入。您尝试部分使用,最后两者都不使用。
@Singleton
@Component(modules = {UserModule.class, BackEndServiceModule.class})
public interface MyComponent {
User user();
BackendService backendService();
void inject(Main main);
// you don't need that if you create the object!
// void injectIntoBackendService(BackendService backendService);
}
如有疑问,请使用构造函数注入。顾名思义......在构造函数中定义依赖项并删除任何其他@Inject
。
@Singleton // add singleton here, use constructor injection!
public class BackendService {
public User user;
private String serverUrl;
@Inject
public BackendService(@Named("serverUrl") String serverUrl, User user) {
this.serverUrl = serverUrl;
this.user = user;
}
}
当您不需要任何进一步的设置时,您也不需要任何@Provides
注释方法,所以请取消provideBackendService(@Named("serverUrl") String serverUrl)
。我们在上面的实现类中指定了范围。
我还写了一篇关于使用Dagger时要记住few basic things的文章。