Class SpringBeanStore

java.lang.Object
org.apache.juneau.commons.inject.BasicBeanStore
org.apache.juneau.rest.server.springboot.SpringBeanStore
All Implemented Interfaces:
AutoCloseable, BeanStore, WritableBeanStore

public class SpringBeanStore extends BasicBeanStore
A bean store that uses Spring bean resolution to find beans if they're not already in this store.

This implementation extends BasicBeanStore and delegates to Spring's ApplicationContext for bean resolution when beans are not found in the local store or parent stores.

Example:

// Create a Spring bean store ApplicationContext appContext = ...; SpringBeanStore beanStore = new SpringBeanStore(appContext, null); // Get bean from Spring context Optional<MyService> service = beanStore.getBean(MyService.class); // Get named bean from Spring context Optional<MyService> namedService = beanStore.getBean(MyService.class, "primary"); // Get all beans of a type from Spring context Map<String, MyService> allServices = beanStore.getBeansOfType(MyService.class);

See Also:
  • Constructor Details

    • SpringBeanStore

      public SpringBeanStore(org.springframework.context.ApplicationContext appContext, BeanStore parent)
      Constructor.

      If appContext is non-null, this constructor also makes sure the process-wide Settings singleton holds a SpringEnvironmentPropertySource for it, so that @Value("{...}") placeholders resolve against the Spring Environment (i.e. against application.yaml, env vars, system properties, etc.). There is one such source per application context, shared by every store built on it and kept at the position of the first registration. It is removed when every store on the context has been cleared, or when the context closes (for a ConfigurableApplicationContext).

      Parameters:
      appContext - The Spring application context used to resolve beans. Can be null (no Spring environment bridge is installed).
      parent - The parent bean store. Can be null for a root store with no parent chain.
  • Method Details

    • clear

      Description copied from class: BasicBeanStore
      Removes all beans from this store.

      This operation only affects this store and does not affect the parent bean store.

      Specified by:
      clear in interface WritableBeanStore
      Overrides:
      clear in class BasicBeanStore
      Returns:
      This object.
    • invalidateEnvironmentCache

      public static boolean invalidateEnvironmentCache(org.springframework.context.ApplicationContext appContext)
      Clears the miss cache of the Spring Environment bridge registered for the given application context.

      Needed only after a Spring property source's contents change in place for a key that is not a valid Spring Boot property name and that Juneau already looked up and missed; see SpringEnvironmentPropertySource.invalidate(). The bridge is shared by every SpringBeanStore on the context, so this affects all of them.

      Parameters:
      appContext - The application context. Can be null.
      Returns:
      true if the context had a registered bridge.
      Since:
      10.0.0
    • getBean

      public <T> Optional<T> getBean(Class<T> beanType)
      Description copied from class: BasicBeanStore
      Returns the unnamed bean of the specified type.

      If no unnamed bean is found in this store, searches the parent store recursively.

      Specified by:
      getBean in interface BeanStore
      Overrides:
      getBean in class BasicBeanStore
      Type Parameters:
      T - The type of bean to return.
      Parameters:
      beanType - The type of bean to return. Must not be null.
      Returns:
      The bean, or Optional.empty() if not found.
    • getBean

      public <T> Optional<T> getBean(Class<T> beanType, String name)
      Description copied from class: BasicBeanStore
      Returns the named bean of the specified type.

      If no bean with the specified name is found in this store, searches the parent store recursively.

      Specified by:
      getBean in interface BeanStore
      Overrides:
      getBean in class BasicBeanStore
      Type Parameters:
      T - The type of bean to return.
      Parameters:
      beanType - The type of bean to return. Must not be null.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      The bean, or Optional.empty() if not found.
    • getBeansOfType

      public <T> Map<String,T> getBeansOfType(Class<T> beanType)
      Description copied from class: BasicBeanStore
      Returns all beans of the specified type, keyed by bean name.

      The map keys are the bean names (empty string for unnamed beans), and the values are the bean instances. Results from the parent store are included first, then beans from this store (which override parent beans with the same name).

      Specified by:
      getBeansOfType in interface BeanStore
      Overrides:
      getBeansOfType in class BasicBeanStore
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type. Must not be null.
      Returns:
      A map of bean names to bean instances. Never null.
    • hasBean

      public boolean hasBean(Class<?> beanType)
      Description copied from class: BasicBeanStore
      Returns true if this store contains at least one unnamed bean of the specified type.

      If not found in this store, searches the parent store recursively.

      Specified by:
      hasBean in interface BeanStore
      Overrides:
      hasBean in class BasicBeanStore
      Parameters:
      beanType - The bean type to check. Must not be null.
      Returns:
      true if this store contains at least one unnamed bean of the specified type.
    • hasBean

      public boolean hasBean(Class<?> beanType, String name)
      Description copied from class: BasicBeanStore
      Returns true if this store contains a bean of the specified type and name.

      If not found in this store, searches the parent store recursively.

      Specified by:
      hasBean in interface BeanStore
      Overrides:
      hasBean in class BasicBeanStore
      Parameters:
      beanType - The bean type to check. Must not be null.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      true if this store contains a bean of the specified type and name.
    • getBeanSupplier

      public <T> Optional<Supplier<T>> getBeanSupplier(Class<T> beanType, String name)
      Description copied from class: BasicBeanStore
      Returns the supplier for a named bean of the specified type.

      If no supplier with the specified name is found in this store, searches the parent store recursively.

      Specified by:
      getBeanSupplier in interface BeanStore
      Overrides:
      getBeanSupplier in class BasicBeanStore
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type. Must not be null.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      The supplier, or Optional.empty() if no supplier of the specified type and name exists.