woodybird
woodybird

Reputation: 135

Dagger official example for Binding Instances problem

I'm new to dagger. I was following the documentation at https://dagger.dev/dev-guide/ and when it comes to Binding Instances section, here's the example provided:

@Component(modules = AppModule.class)
interface AppComponent {
  App app();

  @Component.Builder
  interface Builder {
    @BindsInstance Builder userName(@UserName String userName);
    AppComponent build();
  }
}

My question is where's this @UserName coming from? The explanation leading to this code block is

Perhaps your app takes a single argument representing the user’s name that you’d like to inject as @UserName String. You can add a method annotated @BindsInstance to the component builder to allow that instance to be injected in the component.

I couldn't reason about it. Can anyone shed some light?

Thanks

Upvotes: 0

Views: 558

Answers (1)

David Medenjak
David Medenjak

Reputation: 34552

@Username in this example is a Qualifier. If you were to use/inject a plain String it'd clutter things up quite a lot (since String is a somewhat common type), but in combination with a qualifier @Username String becomes a unique key.

@Named("foo") is a basic qualifier included that you can use, or you can just create your own—e.g. @Username

Upvotes: 1

Related Questions