- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.7k
 
SCOPE_NOT_FOUND
        Googler edited this page Apr 15, 2025 
        ·
        3 revisions
      
    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.
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);
  }
}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.
- 
User's Guide
 - 
Integration
 - 
Extensions
 - 
Internals
 - 
Releases
 - 
Community