Skip to content

SCOPE_NOT_FOUND

Googler edited this page Jun 18, 2021 · 2 revisions

SCOPE_NOT_FOUND

Summary

In addition to built-in scopes like Singleton and RequestScoped, Guice also supports custom scopes. Guice will throw a SCOPE_NOT_FOUND error when a custom scope is used without being registered in the injector.

Common Causes

Using the wrong annotation

Each custom scope is associated with a ScopeAnnotation so make sure that you've imported the correct annotation to use.

Example:

import com.some.third.party.RequestScoped; // Wrong RequestScoped annotation.

final class FooRequestModule extends AbstractModule {
  @Provides
  @RequestScoped
  Foo provideFoo(FooRequest request) {
    return FooFactory.createFoo(request);
  }
}
import com.google.inject.servlet.RequestScoped // Correct RequestScoped annotation.

final class FooRequestModule extends AbstractModule {
  @Provides
  @RequestScoped
  Foo provideFoo(FooRequest request) {
    return FooFactory.createFoo(request);
  }
}

Using the scope without registering it

Custom scope implementations must be registered in the injector, so make sure that you've done so if you get a SCOPE_NOT_FOUND error.

Example:

final class ApplicationModule extends AbstractModule {
  @Provides
  @BatchScoped
  Foo provideFoo() {
    return FooFactory.createFoo();
  }
}
final class ApplicationModule extends AbstractModule {
  @Override
  protected void configure() {
    // BatchScoped is registered in BatchScopeModule and must be installed
    // to use @BatchScoped annotation.
    install(new BatchScopeModule());
  }

  @Provides
  @BatchScoped
  Foo provideFoo() {
    return FooFactory.createFoo();
  }
}

This can often happen in tests that have not installed the module that defines the scope. In some cases, you can fix this by using NO_SCOPE.

Clone this wiki locally