Dagger 2抱怨@Provides方法存在

时间:2019-12-24 20:58:44

标签: java dependency-injection dagger-2

我正在尝试创建一个具有一个类(WhitelistChecker)和一个相应的dagger模块(DynamicConfigProviderModule)的帮助程序包。我在聚合模块(ServiceModule)的服务包中使用它,并将WhitelistChecker帮助器类注入我的服务活动(ServiceActivity)。

---------------------------------------------------------
Helper package
---------------------------------------------------------
public class WhitelistChecker {

    private final ConfigRetriever configRetriever;
    private final ConfigName configName;
    private final int schemaVersion;

    @Inject
    public WhitelistChecker(final ConfigRetriever configRetriever,
                            final ConfigName configName,
                            final int schemaVersion) {
        this.configRetriever = configRetriever;
        this.configName = configName;
        this.schemaVersion = schemaVersion;
    }
}

@Module
public class DynamicConfigProviderModule {

    @Provides
    @Named(DaggerConfig.LOGGING_WHITELIST_CHECKER_NAME)
    public WhitelistChecker providesWhitelistCheckerForLogging(final ConfigRetriever configRetriever,
                                                                 @Named(DaggerConfig.LOGGING_WHITELIST_CONFIG_NAME)
                                                                 final ConfigName configName,
                                                                 final SchemaVersion schemaVersion) {

        return new WhitelistChecker(configRetriever, configName, (int) schemaVersion.getSchemaVersion());
    }

    @Provides
    @Named(DaggerConfig.LOGGING_WHITELIST_CONFIG_NAME)
    public ConfigName providesLoggingWhitelistConfigName() {
        return ConfigName.LOGGING_WHITELIST_CONFIG;
    }

    @Provides
    public SchemaVersion providesSchemaVersion() {
        return SchemaVersion.TWO;
    }

    @Provides
    @Singleton
    public ConfigRetriever providesConfigRetriever(@Named(DaggerConfig.REGION) final String regionName,
                                               final CredentialsProvider credentialsProvider) {

        configRetrieverBuilder configRetrieverBuilder = new SDCLambdaRetrieverBuilder();

        configRetrieverBuilder.setCredentialsProvider(credentialsProvider);
        configRetrieverBuilder.setRegion(regionName);

        return configRetrieverBuilder.build();
    }
}

public final class DaggerConfig {

    public static final String LOGGING_WHITELIST_CHECKER_NAME = "LOGGING_WHITELIST_CHECKER";
    public static final String LOGGING_WHITELIST_CONFIG_NAME = "LOGGING_WHITELIST_CONFIG";
    public static final String REGION = "REGION";

    private DaggerConfig() {}
}

---------------------------------------------------------
Service package
---------------------------------------------------------
@Module(includes = {
        DynamicConfigProviderModule.class
        // Include other modules
})
public class ServiceModule { }

@Singleton
@Component(modules = ServiceModule.class)
public interface ServiceComponent {
    ServiceActivity serviceActivity();
}

public class ServiceActivity {


    @NonNull
    @Named(DaggerConfig.LOGGING_WHITELIST_CHECKER_NAME)
    private final WhitelistChecker whitelistChecker;

    @Inject
    public ServiceActivity(final WhitelistChecker WhitelistChecker) {
        this.WhitelistChecker = WhitelistChecker;
    }
}
  1. 当我尝试构建它时,它给我一个错误,提示error: ConfigName cannot be provided without an @Provides-annotated method.。当我明确提供一种@Provides方法来构造ConfigName时,为什么会发生这种情况?
  2. 如果我提供像-

    这样的@Provides方法
    @Provides
    @Named(DaggerConfig.LOGGING_WHITELIST_CHECKER_NAME)
    public WhitelistChecker providesWhitelistCheckerForLogging(final ConfigRetriever configRetriever) {
    
        return new WhitelistChecker(configRetriever, "LoggingWhitelistConfig",
                (int) SchemaVersion.TWO.getSchemaVersion());
    }
    

    匕首仍然给我同样的错误。当我创建使用ConfigName的对象时,为什么要尝试自己创建@Named

  3. 我希望区域和凭据来自服务软件包,并且我正在使用ServiceActivity将这些参数绑定到软件包中。这是实施此模式的正确/推荐方法吗?

编辑1-1和2的答案 我在错误的位置添加了@Named注释。我正在使用构造函数注入,因此@Named批注需要位于构造函数参数上,而不是字段本身。

public class ServiceActivity { @NonNull // Remove @Named from here and add it on the constructor parameter instead private final WhitelistChecker whitelistChecker; @Inject public ServiceActivity(@Named(DaggerConfig.LOGGING_WHITELIST_CHECKER_NAME) final WhitelistChecker WhitelistChecker) { this.WhitelistChecker = WhitelistChecker; } } -

的正确代码
vm.PostViewModel = db.POSTs
            .Join(db.COMMENTs, b => b.user_id, c => c.user_id, (b, c) => b)
            .Join(db.REPLYs, b => b.user_id, c => c.user_id, (b, c) => b)
            .ToList();

0 个答案:

没有答案