frm
frm

Reputation: 687

Dagger2 not finding annotated methods

I'm building a Java project using Gradle for build the project and Dagger2 as dependency injector. And I'm getting this error:

<path to the class>/MyComponent.java:13: error: [Dagger/MissingBinding] <package>.ConnectionDTO cannot be provided without an @Inject constructor or an @Provides-annotated method.
    ConnectionDTO sourceConnectionDTO();
                  ^
      <package>.ConnectionDTO is provided at
          <package>.MyComponent.sourceConnectionDTO()
<path to the class>/MyComponent.java:14: error: [Dagger/MissingBinding] <package>.ConnectionDTO cannot be provided without an @Inject constructor or an @Provides-annotated method.
    ConnectionDTO destinationConnectionDTO();
                  ^
      <package>.ConnectionDTO is provided at
          <package>.MyComponent.destinationConnectionDTO()

Java version 1.8 Gradle version: 5.4.1 (also tried with 4.5.1)

Dagger dependencies:

dependencies {
    annotationProcessor 'com.google.dagger:dagger-compiler:2.17'

    compile 'com.google.dagger:dagger:2.17'
    ...
}

Module Class:

@Module
public class MyModule {
    private final MyConfiguration config;

    @Inject
    public MetaStoreModule(MyConfiguration config){
        this.config = config;
    }

    @Provides
    @Singleton
    @Named("sourceConnection")
    public ConnectionDTO sourceConnectionDTO() {
        return new ConnectionDTO(config.sourceHost(), config.sourceUser(),
                config.sourcePassword(), config.sourceDataBaseName());
    }

    @Provides
    @Singleton
    @Named("destinationConnection")
    public ConnectionDTO destinationConnectionDTO() {
        return new ConnectionDTO(config.destinationHost(), config.destinationUser(),
                config.destinationPassword(), config.destinationDataBaseName());
    }
}

Component class:

import dagger.Component;

import javax.inject.Singleton;

@Singleton
@Component(modules = MyModule.class)
public interface MyComponent{
    ConnectionDTO sourceConnectionDTO();
    ConnectionDTO destinationConnectionDTO();
}

Not sure why if the methods are annotated they are not being recognised by the build

Upvotes: 0

Views: 29

Answers (1)

David Medenjak
David Medenjak

Reputation: 34542

You're not adding ConnectionDTO anywhere, that's why Dagger complains that it can't find it.

You add @Named("sourceConnection") ConnectionDTO as well as @Named("destinationConnection") ConnectionDTO, so you should also request those.

@Singleton
@Component(modules = MyModule.class)
public interface MyComponent{
    @Named("sourceConnection")
    ConnectionDTO sourceConnectionDTO();

    @Named("destinationConnection")
    ConnectionDTO destinationConnectionDTO();
}

Upvotes: 2

Related Questions