Skip to content

DUPLICATE_SCOPES

Googler edited this page Aug 13, 2020 · 1 revision

DUPLICATE_SCOPES

Summary

Guice will throw a DUPLICATE_SCOPES error if a scope annotation is registered to more than one implementation.

Example:

import com.google.inject.servlet.RequestScoped;

final class MyRequestScopeModule extends AbstractModule {
  @Override
  protected void configure() {
    MyRequestScope scope = new MyRequestScope();
    bindScope(RequestScoped.class, scope);
  }
}

When the above module is installed in the same injector as Guice's ServletModule, which also registers an implementation for RequestScoped, Guice will throw a DUPLICATE_SCOPES error:

import com.google.inject.servlet.ServletModule;

final class ApplicationModule extends AbstraceModule {
  @Override
  protected void configure() {
    install(new MyRequestScopeModule());
    ...
    // ServletModule also registers `RequestScoped` scope.
    install(new ServletModule());
  }
}

Common Causes

Rebinding an existing scope annotation

In the example in summary section, RequestScoped is being registered multiple times. To fix this type of error, you can either:

  • Remove all but one implementation of the scope.
  • Or, use a different scope annotations for each scope implementation.

Installing a scope registering module more than once

If a scope is registered in FooModule and FooModule is installed more than once in the injector then a DUPLICATE_SCOPES (and maybe many other Guice errors) will be thrown. So make sure that FooModule is only installed once.

Clone this wiki locally