Class BeanStore

java.lang.Object
org.apache.juneau.cp.BeanStore
Direct Known Subclasses:
BeanStore.Void, SpringBeanStore

public class BeanStore extends Object
Java bean store.

A simple storage database for beans keyed by type and name. Used to retrieve and instantiate beans using an injection-like API. It's similar in concept to the injection framework of Spring but greatly simplified in function and not intended to implement a full-fledged injection framework.

Beans can be stored with or without names. Named beans are typically resolved using the @Named or @Qualified annotations on constructor or method parameters.

Beans are added through the following methods:

Beans are retrieved through the following methods:

Beans are created through the following methods:

Notes:
See Also:
  • Field Details

    • INSTANCE

      public static final BeanStore INSTANCE
      Static read-only reusable instance.
  • Constructor Details

    • BeanStore

      protected BeanStore(BeanStore.Builder builder)
      Constructor.
      Parameters:
      builder - The builder containing the settings for this bean.
  • Method Details

    • create

      public static BeanStore.Builder create()
      Static creator.
      Returns:
      A new BeanStore.Builder object.
    • of

      public static BeanStore of(BeanStore parent)
      Static creator.
      Parameters:
      parent - Parent bean store. Can be null if this is the root resource.
      Returns:
      A new BeanStore object.
    • of

      public static BeanStore of(BeanStore parent, Object outer)
      Static creator.
      Parameters:
      parent - Parent bean store. Can be null if this is the root resource.
      outer - The outer bean used when instantiating inner classes. Can be null.
      Returns:
      A new BeanStore object.
    • addBean

      public <T> BeanStore addBean(Class<T> beanType, T bean)
      Adds an unnamed bean of the specified type to this factory.
      Type Parameters:
      T - The class to associate this bean with.
      Parameters:
      beanType - The class to associate this bean with.
      bean - The bean. Can be null.
      Returns:
      This object.
    • addBean

      public <T> BeanStore addBean(Class<T> beanType, T bean, String name)
      Adds a named bean of the specified type to this factory.
      Type Parameters:
      T - The class to associate this bean with.
      Parameters:
      beanType - The class to associate this bean with.
      bean - The bean. Can be null.
      name - The bean name if this is a named bean. Can be null.
      Returns:
      This object.
    • addSupplier

      public <T> BeanStore addSupplier(Class<T> beanType, Supplier<T> bean)
      Adds a supplier for an unnamed bean of the specified type to this factory.
      Type Parameters:
      T - The class to associate this bean with.
      Parameters:
      beanType - The class to associate this bean with.
      bean - The bean supplier.
      Returns:
      This object.
    • addSupplier

      public <T> BeanStore addSupplier(Class<T> beanType, Supplier<T> bean, String name)
      Adds a supplier for a named bean of the specified type to this factory.
      Type Parameters:
      T - The class to associate this bean with.
      Parameters:
      beanType - The class to associate this bean with.
      bean - The bean supplier.
      name - The bean name if this is a named bean. Can be null.
      Returns:
      This object.
    • add

      public <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 class to associate this bean with.
      Parameters:
      beanType - The class to associate this bean with.
      bean - The bean. Can be null.
      Returns:
      The bean.
    • add

      public <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 class to associate this bean with.
      Parameters:
      beanType - The class to associate this bean with.
      bean - The bean. Can be null.
      name - The bean name if this is a named bean. Can be null.
      Returns:
      The bean.
    • clear

      public BeanStore clear()
      Clears out all bean in this bean store.

      Does not affect the parent bean store.

      Returns:
      This object.
    • getBean

      public <T> Optional<T> getBean(Class<T> beanType)
      Returns the unnamed bean of the specified type.
      Type Parameters:
      T - The type of bean to return.
      Parameters:
      beanType - The type of bean to return.
      Returns:
      The bean.
    • getBean

      public <T> Optional<T> getBean(Class<T> beanType, String name)
      Returns the named bean of the specified type.
      Type Parameters:
      T - The type of bean to return.
      Parameters:
      beanType - The type of bean to return.
      name - The bean name. Can be null.
      Returns:
      The bean.
    • stream

      public <T> Stream<BeanStoreEntry<T>> stream(Class<T> beanType)
      Returns all the beans in this store of the specified type.

      Returns both named and unnamed beans.

      The results from the parent bean store are appended to the list of beans from this beans store.

      Type Parameters:
      T - The bean type to return.
      Parameters:
      beanType - The bean type to return.
      Returns:
      The bean entries. Never null.
    • removeBean

      public BeanStore removeBean(Class<?> beanType)
      Removes an unnamed bean from this store.
      Parameters:
      beanType - The bean type being removed.
      Returns:
      This object.
    • removeBean

      public BeanStore removeBean(Class<?> beanType, String name)
      Removes a named bean from this store.
      Parameters:
      beanType - The bean type being removed.
      name - The bean name to remove.
      Returns:
      This object.
    • hasBean

      public boolean hasBean(Class<?> beanType)
      Returns true if this store contains the specified unnamed bean type.
      Parameters:
      beanType - The bean type to check.
      Returns:
      true if this store contains the specified unnamed bean type.
    • hasBean

      public boolean hasBean(Class<?> beanType, String name)
      Returns true if this store contains the specified named bean type.
      Parameters:
      beanType - The bean type to check.
      name - The bean name.
      Returns:
      true if this store contains the specified named bean type.
    • createBean

      public <T> BeanCreator<T> createBean(Class<T> beanType)
      Instantiates a bean creator.
      See Also:
      Type Parameters:
      T - The bean type to create.
      Parameters:
      beanType - The bean type to create.
      Returns:
      A new bean creator.
    • createMethodFinder

      public <T> BeanCreateMethodFinder<T> createMethodFinder(Class<T> beanType, Object resource)
      Create a method finder for finding bean creation methods.
      See Also:
      Type Parameters:
      T - The bean type to create.
      Parameters:
      beanType - The bean type to create.
      resource - The class containing the bean creator method.
      Returns:
      The method finder. Never null.
    • createMethodFinder

      public <T> BeanCreateMethodFinder<T> createMethodFinder(Class<T> beanType, Class<?> resourceClass)
      Create a method finder for finding bean creation methods.

      Same as createMethodFinder(Class,Class) but looks for only static methods on the specified resource class and not also instance methods within the context of a bean.

      See Also:
      Type Parameters:
      T - The bean type to create.
      Parameters:
      beanType - The bean type to create.
      resourceClass - The class containing the bean creator method.
      Returns:
      The method finder. Never null.
    • createMethodFinder

      public <T> BeanCreateMethodFinder<T> createMethodFinder(Class<T> beanType)
      Create a method finder for finding bean creation methods.

      Same as createMethodFinder(Class,Object) but uses BeanStore.Builder.outer(Object) as the resource bean.

      See Also:
      Type Parameters:
      T - The bean type to create.
      Parameters:
      beanType - The bean type to create.
      Returns:
      The method finder. Never null.
    • getMissingParams

      public String getMissingParams(ExecutableInfo executable)
      Given an executable, returns a list of types that are missing from this factory.
      Parameters:
      executable - The constructor or method to get the params for.
      Returns:
      A comma-delimited list of types that are missing from this factory, or null if none are missing.
    • hasAllParams

      public boolean hasAllParams(ExecutableInfo executable)
      Given the list of param types, returns true if this factory has all the parameters for the specified executable.
      Parameters:
      executable - The constructor or method to get the params for.
      Returns:
      A comma-delimited list of types that are missing from this factory.
    • getParams

      public Object[] getParams(ExecutableInfo executable)
      Returns the corresponding beans in this factory for the specified param types.
      Parameters:
      executable - The constructor or method to get the params for.
      Returns:
      The corresponding beans in this factory for the specified param types.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createEntry

      protected <T> BeanStoreEntry<T> createEntry(Class<T> type, Supplier<T> bean, String name)
      Creates an entry in this store for the specified bean.

      Subclasses can override this method to create their own entry subtypes.

      Type Parameters:
      T - The class type to associate with the bean.
      Parameters:
      type - The class type to associate with the bean.
      bean - The bean supplier.
      name - Optional name to associate with the bean. Can be null.
      Returns:
      A new bean store entry.