简体   繁体   English

匕首2:如何注入在@Provides方法中创建的对象?

[英]Dagger 2: How to inject an object created within a @Provides method?

I've got this: 我有这个:

Bike.java Bike.java

public class Bike {
    String serial;

    @Inject
    Wheels wheels;

    public Bike(String serial) {
        this.serial = serial;
    }
}

BikeModule.java BikeModule.java

@Module
public class BikeModule {
    @Provides
    public Bike provideBike() {
        return new Bike("BIK-001");
    }

    @Provides
    public Wheels provideWheels() {
        return new Wheels("WLS-027");
    }
}

BikeComponent.java BikeComponent.java

@Component(modules = BikeModule.class)
public interface BikeComponent {
    Bike bike();
}

Now here's the problem: when I call BikeComponent.bike() , I get a bike with the serial BIK-001 , as intended, but the wheels are not injected. 现在是问题所在:当我调用BikeComponent.bike() ,按预期得到了带有序列号BIK-001的自行车,但未注入车轮。 However, if I annotate the Bike constructor with @Inject and remove the BikeModule.provideBike() method, the wheels do get injected. 但是,如果我用@Inject注释Bike构造函数并删除BikeModule.provideBike()方法,则车轮确实会被注入。 So the problem seems to be about injecting an object created within a @Provides method, instead of created by Dagger itself. 因此,问题似乎出在注入在@Provides方法中创建的对象,而不是Dagger本身创建的对象。

Is there a way to tell Dagger to inject a provided object? 有没有办法告诉Dagger注入提供的对象?

Rewrite like this: 像这样重写:

public class Bike {

    private final String serial;
    private final Wheels wheels;

    @Inject
    public Bike(String serial, Wheels wheels) {
        this.serial = serial;
        this.wheels = wheels;
    }
}

@Module
public final class BikeModule {

    @Provides
    public static Bike provideBike(Wheels wheels) {
        return new Bike("BIK-001", wheels);
    }

    @Provides
    public static Wheels provideWheels() {
        return new Wheels("WLS-027");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 dagger 错误 - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供字符串 - How to resolve dagger error - String cannot be provided without an @Inject constructor or an @Provides-annotated method 匕首2俯瞰提供方法 - Dagger 2 is overlooking provides method 没有@Inject构造函数或@Provides注释方法无法提供-Dagger 2 - cannot be provided without an @Inject constructor or from an @Provides-annotated method - Dagger 2 Dagger 2错误:如果没有@inject构造函数或@ provide-annotated方法,则无法提供 - Dagger 2 error: cannot be provided without an @inject constructor or from an @provides-annotated method Dagger2-没有@Inject构造函数或@Provides注释方法无法提供 - Dagger2 - cannot be provided without an @Inject constructor or from an @Provides-annotated method dagger2错误“没有@Inject构造函数或来自@ Provide-annotated方法,不能提供android.app.Application” - dagger2 error “android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method” Dagger 2.10:子组件+自定义范围=“不能在没有@Inject构造函数或@ Provide-或@ Produces-annotated方法的情况下提供” - Dagger 2.10: Subcomponent + Custom scope = “cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method” 在Dagger中的@Provides方法内使用注入 - Using injection inside a @Provides method in Dagger Dagger 2 - two提供了提供相同接口的方法 - Dagger 2 - two provides method that provide same interface 如何在Dagger 2中的对象图中注入一个已经存在的对象? - How can I inject an already existing object into the object graph in Dagger 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM