问题描述
我是Dagger的CDI新手。我有以下结构。问题是当我去获取Main类中的backendService时,仅提取了直接的backendService,但基础的User依赖项保持为null。
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
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 + "]";
}
}
Class UserModule
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
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
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.
@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
。
When in doubt, use constructor injection. As the name suggests..define dependencies in the constructor and get rid of any other @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)
。我们在上面的实现类上指定了范围。
And when you don't need any further setup you don't need any @Provides
annotated methods either, so get rid of provideBackendService(@Named("serverUrl") String serverUrl)
please. We specified the scope on the implementation class above.
我还写了一篇有关。
这篇关于字段@Inject在Dagger2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!