Dagger2 模块未注入其他模块或已弃用

Dagger2 Module not getting injected in other module or is deprecated

我正在尝试学习 Dagger2,但模块即将弃用。 我正在关注 http://www.vogella.com/tutorials/Dagger/article.html 的教程 这是我的代码如下。

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

@Module
public class UserModule {

    @Provides
    @Singleton
    User providesUser() {
        return new User("John", "Doe");
    }
}

后端服务

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;
        }
    }

}

后端服务模块

@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";
    }

}

后端组件

@Singleton
@Component(modules = {UserModule.class, BackendServiceModule.class})
public interface BackendComponent {


    BackendService proviedBackendServiceModule();

    void inject(TestApp app);

}

我在 运行 之后得到如下输出:

User: null
ServerUrl: http://www.google.com

如果我尝试按照代码运行

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]

你的问题的原因是匕首的工作方式。它仅在您指定为注入方法参数的 class 上注入用 @Inject 注释的字段。因此,不会注入带有 @Inject 注释的 BackendService 中的任何内容。

public class TestApp {

    @Inject <-- this is injected
    User user;
    @Inject <-- this is injected
    BackendService backendService; <-- Nothing inside this class will be injected

我建议将您的用户对象传递给构造函数中的 BackendService。只要做这样的事情:

 @Provides
    @Singleton
    BackendService proviedBackendServiceModule(@Named("serverUrl") String serverUrl, User user) {
        return new BackendService(serverUrl, user);
    }

另一种解决方案是在您的组件内为 BackendService 定义一个注入方法,但它会在组件对象周围传递时变得混乱。