Interface Scope

All Known Implementing Classes:
MojoExecutionScope, MojoExecutionScope, SessionScope, SessionScope

@Experimental public interface Scope
Defines how object instances are managed within a specific scope.

A Scope controls the lifecycle and visibility of objects created by the injector. It determines when new instances are created and when existing instances can be reused. This allows for different caching strategies depending on the desired lifecycle of the objects.

Example implementation for a simple caching scope:

 public class CachingScope implements Scope {
     private final Map<Key<?>, Object> cache = new ConcurrentHashMap<>();

     @Override
     public <T> Supplier<T> scope(Key<T> key, Supplier<T> unscoped) {
         return () -> {
             return (T) cache.computeIfAbsent(key, k -> unscoped.get());
         };
     }
 }
 
Since:
4.0.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> Supplier<T>
    scope(Key<T> key, Supplier<T> unscoped)
    Scopes a supplier of instances.
  • Method Details

    • scope

      @Nonnull <T> Supplier<T> scope(@Nonnull Key<T> key, @Nonnull Supplier<T> unscoped)
      Scopes a supplier of instances.

      This method wraps an unscoped instance supplier with scope-specific logic that controls when new instances are created versus when existing instances are reused.

      Type Parameters:
      T - the type of instance being scoped
      Parameters:
      key - the key identifying the instance type
      unscoped - the original unscoped instance supplier
      Returns:
      a scoped supplier that implements the scope's caching strategy
      Throws:
      NullPointerException - if key or unscoped is null