Interface WritableBeanStore
- All Superinterfaces:
AutoCloseable,BeanStore
- All Known Implementing Classes:
BasicBeanStore,SpringBeanStore,TestBeanStore
BeanStore with methods for adding beans.
This interface provides write operations for bean stores, allowing beans to be added either as direct instances or as suppliers for lazy initialization.
Example:
WritableBeanStore
See Also:
BeanStore- Read-only bean lookup interface
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> TSame asaddBean(Class,Object)but returns the bean instead of this object for fluent calls.default <T> TSame asaddBean(Class,Object,String)but returns the bean instead of this object for fluent calls.Adds an unnamed bean of the specified type to this store.Adds a named bean of the specified type to this store.addBeanType(Class<T> beanType, Class<? extends T> implType) Registers a default implementation class for the specified bean type.addDefaultSupplier(Class<T> beanType, Supplier<T> supplier) Adds a fallback supplier for an unnamed bean of the specified type to this store.addDefaultSupplier(Class<T> beanType, Supplier<T> supplier, String name) Adds a fallback supplier for a named bean of the specified type to this store.addSupplier(Class<T> beanType, Supplier<T> supplier) Adds a supplier for an unnamed bean of the specified type to this store.addSupplier(Class<T> beanType, Supplier<T> supplier, String name) Adds a supplier for a named bean of the specified type to this store.clear()Removes all beans from this store.voidclose()Closes this bean store, invoking pre-destroy callbacks.booleanhasDefaultSupplier(Class<?> beanType) Returnstrue if this store has a default supplier registered locally for the specified unnamed bean type.booleanhasDefaultSupplier(Class<?> beanType, String name) Returnstrue if this store has a default supplier registered locally for the specified bean type and name.voidpopOverlay(Snapshot snapshot) Removes the overlay frame identified by the suppliedSnapshot.pushOverlay(BeanStore overlay) Pushes the suppliedBeanStoreonto this store's overlay stack.registerConfiguration(Class<?> configType) Registers a configuration class and itsBean-annotated members.default WritableBeanStoreregisterConfigurations(Class<?>... configTypes) Registers multiple configuration classes.Methods inherited from interface org.apache.juneau.commons.inject.BeanStore
createBeanFromMethod, createBeanFromMethod, getBean, getBean, getBeansOfType, getBeanSupplier, getBeanSupplier, getBeanType, getMissingParams, getParams, hasAllParams, hasBean, hasBean, instantiate, invokeBeanMethod
-
Method Details
-
add
Same asaddBean(Class,Object)but returns the bean instead of this object for fluent calls.- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.bean- The bean instance. Can benull (stored as an explicitnull binding, distinct from no binding at all).- Returns:
- The bean.
-
add
Same asaddBean(Class,Object,String)but returns the bean instead of this object for fluent calls.- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.bean- The bean instance. Can benull (stored as an explicitnull binding, distinct from no binding at all).name- The bean name. Can benull for unnamed beans.- Returns:
- The bean.
-
addBean
Adds an unnamed bean of the specified type to this store.- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.bean- The bean instance. Can benull (stored as an explicitnull binding, distinct from no binding at all).- Returns:
- This object for method chaining.
-
addBean
Adds a named bean of the specified type to this store.- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.bean- The bean instance. Can benull (stored as an explicitnull binding, distinct from no binding at all).name- The bean name. Can benull for unnamed beans.- Returns:
- This object for method chaining.
-
addSupplier
Adds a supplier for an unnamed bean of the specified type to this store.The supplier will be invoked lazily when the bean is first requested.
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.supplier- The bean supplier. Must not benull .- Returns:
- This object for method chaining.
-
addSupplier
Adds a supplier for a named bean of the specified type to this store.The supplier will be invoked lazily when the bean is first requested.
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.supplier- The bean supplier. Must not benull .name- The bean name. Can benull for unnamed beans.- Returns:
- This object for method chaining.
-
addDefaultSupplier
Adds a fallback supplier for an unnamed bean of the specified type to this store.Default suppliers are consulted only after regular entries and the regular parent chain have been searched. They serve as a "use this if nothing else has provided one" hook, useful for memoizer-backed framework defaults that should not shadow explicit user registrations or beans inherited from an overriding parent (e.g. a Spring application context).
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.supplier- The bean supplier. Must not benull .- Returns:
- This object for method chaining.
-
addDefaultSupplier
Adds a fallback supplier for a named bean of the specified type to this store.Default suppliers are consulted only after regular entries and the regular parent chain have been searched. See
addDefaultSupplier(Class,Supplier).- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.supplier- The bean supplier. Must not benull .name- The bean name. Can benull for unnamed beans.- Returns:
- This object for method chaining.
-
clear
Removes all beans from this store.This operation only affects this store and does not affect any parent stores that may be associated with this store.
- Returns:
- This object for method chaining.
-
hasDefaultSupplier
Returnstrue if this store has a default supplier registered locally for the specified unnamed bean type.This is intended for callers that need to distinguish "framework default present" from "any binding exists" — for example, REST initialization uses this signal to skip types that are managed by an internal memoizer. Parent and overriding-parent stores are not consulted.
- Parameters:
beanType- The bean type to check.- Returns:
true if a default supplier for the unnamed bean type is registered on this store.
-
hasDefaultSupplier
Returnstrue if this store has a default supplier registered locally for the specified bean type and name.See
hasDefaultSupplier(Class)for the rationale. Parent and overriding-parent stores are not consulted.- Parameters:
beanType- The bean type to check.name- The bean name. Can benull for unnamed beans.- Returns:
true if a default supplier for the bean type and name is registered on this store.
-
addBeanType
Registers a default implementation class for the specified bean type.Type bindings are consulted by callers (notably
BeanInstantiatorand the various REST memoizers) when resolving the impl class to instantiate forbeanType . This is the type-binding analogue ofaddDefaultSupplier: it provides a deferred-construction default that can be overridden by later resolution steps (e.g. an annotation override).- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type to bind.implType- The implementation class to instantiate when this type is resolved.- Returns:
- This object for method chaining.
-
registerConfiguration
Registers a configuration class and itsBean-annotated members.- Parameters:
configType- The configuration type.- Returns:
- This object.
-
registerConfigurations
Registers multiple configuration classes.- Parameters:
configTypes- The configuration types. Can benull , in which case no action is taken.- Returns:
- This object.
-
pushOverlay
Pushes the suppliedBeanStoreonto this store's overlay stack.The pushed frame becomes the top-priority lookup target for subsequent bean resolution — it sits above any pre-existing
overridingParentsetting, this store's local entries, the regular parent chain, and the default-supplier tier. Frames are popped in LIFO order viapopOverlay(Snapshot).This is the framework primitive behind Mode OVERLAY of the test-injection model: a long-lived SUT's bean store has a per-test overlay pushed at
@BeforeEach, then popped at@AfterEach, so subsequent tests see the original beans again with no rebuild cost.Composition with
overridingParent:When this store was constructed with an overriding parent and has overlays pushed on top, the overlay stack wins (it is "more recent"). Both are still consulted before local entries and the regular parent.
- Parameters:
overlay- The bean store to layer on top. Must not benull .- Returns:
- A
Snapshotthat must be passed back topopOverlay(Snapshot)to remove the frame. - Throws:
NullPointerException- Ifoverlayisnull .- Since:
- 10.0.0
-
popOverlay
Removes the overlay frame identified by the suppliedSnapshot.Pops must be issued in LIFO order — the supplied snapshot must identify the current top-of-stack frame. Out-of-order pops, pops on an empty stack, and pops of a snapshot produced by a different store all throw
IllegalStateExceptionwith a descriptive message. These are programming errors, surfaced loudly rather than silently degraded.- Parameters:
snapshot- The snapshot returned by a priorpushOverlay(BeanStore)call. Must not benull .- Throws:
NullPointerException- Ifsnapshotisnull .IllegalStateException- If the snapshot was produced by a different store, the overlay stack is empty, or the snapshot does not identify the top-of-stack frame (LIFO violation).- Since:
- 10.0.0
-
close
Closes this bean store, invoking pre-destroy callbacks.- Specified by:
closein interfaceAutoCloseable- Throws:
BeanCreationException- on shutdown failures.
-