Interface WritableBeanStore

All Superinterfaces:
AutoCloseable, BeanStore
All Known Implementing Classes:
BasicBeanStore, SpringBeanStore, TestBeanStore

public interface WritableBeanStore extends BeanStore, AutoCloseable
Extends 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 beanStore = ...; // Add unnamed bean beanStore.addBean(MyService.class, new MyService()); // Add named bean beanStore.addBean(MyService.class, new MyService(), "primary"); // Add bean supplier for lazy initialization beanStore.addSupplier(MyService.class, () -> new MyService()); // Add named bean supplier beanStore.addSupplier(MyService.class, () -> new MyService(), "secondary"); // Clear all beans beanStore.clear();

See Also:
  • BeanStore - Read-only bean lookup interface
  • Method Details

    • add

      default <T> T add(Class<T> beanType, T bean)
      Same as addBean(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 be null (stored as an explicit null binding, distinct from no binding at all).
      Returns:
      The bean.
    • add

      default <T> T add(Class<T> beanType, T bean, String name)
      Same as addBean(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 be null (stored as an explicit null binding, distinct from no binding at all).
      name - The bean name. Can be null for unnamed beans.
      Returns:
      The bean.
    • addBean

      <T> WritableBeanStore addBean(Class<T> beanType, T bean)
      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 be null (stored as an explicit null binding, distinct from no binding at all).
      Returns:
      This object for method chaining.
    • addBean

      <T> WritableBeanStore addBean(Class<T> beanType, T bean, String name)
      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 be null (stored as an explicit null binding, distinct from no binding at all).
      name - The bean name. Can be null for unnamed beans.
      Returns:
      This object for method chaining.
    • addSupplier

      <T> WritableBeanStore addSupplier(Class<T> beanType, Supplier<T> supplier)
      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 be null.
      Returns:
      This object for method chaining.
    • addSupplier

      <T> WritableBeanStore addSupplier(Class<T> beanType, Supplier<T> supplier, String name)
      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 be null.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      This object for method chaining.
    • addDefaultSupplier

      <T> WritableBeanStore addDefaultSupplier(Class<T> beanType, Supplier<T> supplier)
      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 be null.
      Returns:
      This object for method chaining.
    • addDefaultSupplier

      <T> WritableBeanStore addDefaultSupplier(Class<T> beanType, Supplier<T> supplier, String name)
      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 be null.
      name - The bean name. Can be null 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

      boolean hasDefaultSupplier(Class<?> beanType)
      Returns true 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

      boolean hasDefaultSupplier(Class<?> beanType, String name)
      Returns true 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 be null for unnamed beans.
      Returns:
      true if a default supplier for the bean type and name is registered on this store.
    • addBeanType

      <T> WritableBeanStore addBeanType(Class<T> beanType, Class<? extends T> implType)
      Registers a default implementation class for the specified bean type.

      Type bindings are consulted by callers (notably BeanInstantiator and the various REST memoizers) when resolving the impl class to instantiate for beanType. This is the type-binding analogue of addDefaultSupplier: 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 its Bean-annotated members.
      Parameters:
      configType - The configuration type.
      Returns:
      This object.
    • registerConfigurations

      default WritableBeanStore registerConfigurations(Class<?>... configTypes)
      Registers multiple configuration classes.
      Parameters:
      configTypes - The configuration types. Can be null, in which case no action is taken.
      Returns:
      This object.
    • pushOverlay

      Pushes the supplied BeanStore onto this store's overlay stack.

      The pushed frame becomes the top-priority lookup target for subsequent bean resolution — it sits above any pre-existing overridingParent setting, this store's local entries, the regular parent chain, and the default-supplier tier. Frames are popped in LIFO order via popOverlay(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 be null.
      Returns:
      A Snapshot that must be passed back to popOverlay(Snapshot) to remove the frame.
      Throws:
      NullPointerException - If overlay is null.
      Since:
      10.0.0
    • popOverlay

      void popOverlay(Snapshot snapshot)
      Removes the overlay frame identified by the supplied Snapshot.

      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 IllegalStateException with a descriptive message. These are programming errors, surfaced loudly rather than silently degraded.

      Parameters:
      snapshot - The snapshot returned by a prior pushOverlay(BeanStore) call. Must not be null.
      Throws:
      NullPointerException - If snapshot is null.
      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:
      close in interface AutoCloseable
      Throws:
      BeanCreationException - on shutdown failures.