Class ScopedValueScope
java.lang.Object
build.codemodel.dependency.injection.ScopedValueScope
- All Implemented Interfaces:
Scope
A
Scope backed by a ScopedValue. Within a single run(Runnable) or call(ScopedValue.CallableOp)
invocation, every resolution of a scoped binding returns the same cached instance. The per-scope
instance cache is automatically discarded when the invocation completes — no manual cleanup
required.
Example usage:
final var requestScope = new ScopedValueScope();
framework.bindScope(RequestScoped.class, requestScope);
requestScope.run(() -> {
// every resolution of a @RequestScoped type returns the same instance here
var service = context.create(MyService.class);
});
Compared to a ThreadLocal-backed scope, this scope:
- works safely with virtual threads — no per-thread storage overhead
- cleans up automatically when the scope block exits — no memory leaks
- requires an explicit scope boundary via
run(Runnable)orcall(ScopedValue.CallableOp)
- Since:
- Apr-2026
- Author:
- reed.vonredwitz
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T, X extends Throwable>
Tcall(ScopedValue.CallableOp<T, X> action) Enters the scope, executesaction, and returns its result.voidEnters the scope and executesaction.Binding<?> scope(ValueBinding<?> binding) Wraps the given unscopedValueBindingin a scopedBinding.
-
Constructor Details
-
ScopedValueScope
public ScopedValueScope()
-
-
Method Details
-
run
Enters the scope and executesaction. Withinaction, every resolution of a binding registered under this scope returns the same cached instance. The cache is discarded whenactioncompletes.- Parameters:
action- the action to run within this scope
-
call
Enters the scope, executesaction, and returns its result. Behaves identically torun(Runnable)but allows a value to be returned from the scope.- Type Parameters:
T- the return typeX- the exception type thatactionmay throw- Parameters:
action- the action to call within this scope- Returns:
- the result produced by
action - Throws:
X- ifactionthrows
-
scope
Description copied from interface:ScopeWraps the given unscopedValueBindingin a scopedBinding. The returned binding may cache, share, or re-create instances according to the scope's strategy.The signature uses wildcards so that implementations may be expressed as lambdas. The framework guarantees that the returned
Bindingproduces values that are assignment-compatible with the type represented byBinding.dependency().- Specified by:
scopein interfaceScope- Parameters:
binding- the unscopedValueBindingwhoseValueBinding.value()creates a fresh instance on each call- Returns:
- a scoped
Bindingimplementing this scope's instance lifecycle
-