Skip to content

DUPLICATE_BINDING_ANNOTATIONS

Googler edited this page Mar 8, 2021 · 1 revision

DUPLICATE_BINDING_ANNOTATIONS

Summary

Guice uses binding annotations to create unique Guice keys when creating bindings. Guice will throw DUPLICATE_BINDING_ANNOTATIONS error when there are more than one binding annotation used on a binding.

Example:

final class FooModule extends AbstractModule {
  @Qualifier
  @Retention(RUNTIME)
  @interface Bar {}

  @Qualifier
  @Retention(RUNTIME)
  @interface Baz {}

  @Provides
  @Bar
  @Baz
  Foo provideFoo() {
    ...
  }
}

Guice will throw a DUPLICATE_BINDING_ANNOTATION error when FooModule is used because provideFoo has two binding annotations.

Common Causes

Copy paste error

It's usually a copy paste error when multiple binding annotations are applied to a single binding. Simply remove all but one binding annotation will fix the error.

Binding with multiple keys

If the intention is to create two keys that are bound to the same thing, you can do so by adding a linked binding using a @Provides method:

final class FooModule extends AbstractModule {
  @Qualifier
  @Retention(RUNTIME)
  @interface Bar {}

  @Qualifier
  @Retention(RUNTIME)
  @interface Baz {}

  @Provides
  @Bar
  Foo provideFoo() {
    ...
  }

  @Provides
  @Baz
  Foo provideBazFoo(@Bar Foo foo) {
    return foo;
  }
}

With the above example, @Bar Foo and @Baz Foo both bind to the same thing.

Clone this wiki locally