Interface BeanStore

All Known Subinterfaces:
WritableBeanStore
All Known Implementing Classes:
BasicBeanStore, SpringBeanStore, StackOverlay, TestBeanStore

public interface BeanStore
Spring-bean-like interface for looking up beans by type and name.

This interface provides a minimal contract for bean lookup operations, similar to Spring's BeanFactory interface.

Implementations of this interface can be backed by various sources:

  • Spring's ApplicationContext
  • Custom in-memory stores
  • Other dependency injection frameworks
Example:

BeanStore beanStore = ...; // Get bean by type Optional<MyService> service = beanStore.getBean(MyService.class); // Get bean by name Optional<Object> bean = beanStore.getBean("myBean"); // Get bean by type and name Optional<MyService> namedService = beanStore.getBean(MyService.class, "primary"); // Get all beans of a type Map<String, MyService> allServices = beanStore.getBeansOfType(MyService.class);

See Also:
  • Method Details

    • getBean

      <T> Optional<T> getBean(Class<T> beanType)
      Returns a bean of the specified type.

      If multiple beans of the specified type exist, the behavior is implementation-dependent. Typically, implementations will return the primary bean or throw an exception.

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      Returns:
      The bean, or Optional.empty() if no bean of the specified type exists.
    • getBean

      <T> Optional<T> getBean(Class<T> beanType, String name)
      Returns a bean of the specified type and name.
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      The bean, or Optional.empty() if no bean of the specified type and name exists.
    • getBeansOfType

      <T> Map<String,T> getBeansOfType(Class<T> beanType)
      Returns all beans of the specified type, keyed by bean name.

      The map keys are the bean names, and the values are the bean instances. If no beans of the specified type exist, returns an empty map.

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      Returns:
      A map of bean names to bean instances. Never null.
    • hasBean

      boolean hasBean(Class<?> beanType)
      Returns true if this store contains at least one bean of the specified type.
      Parameters:
      beanType - The bean type.
      Returns:
      true if this store contains at least one bean of the specified type.
    • hasBean

      boolean hasBean(Class<?> beanType, String name)
      Returns true if this store contains a bean of the specified type and name.
      Parameters:
      beanType - The bean type.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      true if this store contains a bean of the specified type and name.
    • instantiate

      default <T> T instantiate(Class<T> type)
      Convenience shortcut for instantiating a bean of the specified type using this store.

      Equivalent to:

      BeanInstantiator.of(type, this).run()

      Use this when you don't need any of BeanInstantiator's configuration options (subtype, factory-method names, builder hooks, etc.). For more control over instantiation, use BeanInstantiator.of(Class, BeanStore) directly.

      Example:

      // Instantiate a bean using this store for parameter resolution MyService service = beanStore.instantiate(MyService.class);

      Type Parameters:
      T - The bean type.
      Parameters:
      type - The bean type to instantiate. Must not be null.
      Returns:
      The instantiated bean.
    • getBeanSupplier

      <T> Optional<Supplier<T>> getBeanSupplier(Class<T> beanType)
      Returns the supplier for an unnamed bean of the specified type.

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

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      Returns:
      The supplier, or Optional.empty() if no supplier of the specified type exists.
    • getBeanSupplier

      <T> Optional<Supplier<T>> getBeanSupplier(Class<T> beanType, String name)
      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.

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      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.
    • createBeanFromMethod

      default <T> Optional<T> createBeanFromMethod(Class<T> beanType, Object onClassOrObject, Predicate<MethodInfo> filter, Object... extraBeans)
      Finds and invokes a factory method that produces a bean of type beanType.

      Scans the methods of the resource class identified by onClassOrObject:

      • If onClassOrObject is a Class, only static methods are eligible and the instance parameter passed to the invoked method is null.
      • If onClassOrObject is any other object, both instance and static methods on its class are eligible, and the object itself is passed as the receiver.

      The search has two passes. The first pass considers public methods only. If it finds nothing, a second pass considers non-public methods (protected, package-private or private) across the class hierarchy, child class first, but only those that carry @Bean (declared directly or inherited from a method they override; see BeanAnnotation.find(MethodInfo)). A non-public method without @Bean is never invoked, even when filter is null. Non-public methods are made accessible before invocation.

      The first method satisfying all of the following is invoked:

      1. Not deprecated.
      2. Return type is assignment-compatible with beanType.
      3. Accepted by filter (if non-null; otherwise any method qualifies).
      4. All parameters can be resolved from this store plus any extraBeans.

      The default implementation returns Optional.empty(). Override in concrete stores (see BasicBeanStore) to enable factory-method scanning.

      Example:

      // Filter only beanStore.createBeanFromMethod(RichLogger.class, resource, m -> m.hasAnnotation(Bean.class)) .ifPresent(creator::impl); // Filter + extra bean not yet in the store beanStore.createBeanFromMethod(EncoderSet.class, resource, m -> m.hasAnnotation(Bean.class), builder) .ifPresent(x -> builder.impl(x)); // No filter, no extra beans beanStore.createBeanFromMethod(RichLogger.class, resource);

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The type of bean to create. Must not be null.
      onClassOrObject - The object instance or Class whose methods are searched. Must not be null.
      filter - Optional predicate restricting which methods are eligible. Can be null (any method qualifies).
      extraBeans - Optional bean instances visible to parameter resolution for this call only. These are not registered in the store.
      Returns:
      The created bean wrapped in an Optional, or Optional.empty() if no matching factory method was found.
      Throws:
      BeanCreationException - If a matching method was found but threw an exception during invocation.
      See Also:
    • invokeBeanMethod

      default <T> Optional<T> invokeBeanMethod(Class<T> beanType, MethodInfo method, Object onClassOrObject, Object... extraBeans)
      Invokes one specific factory method to produce a bean of type beanType.

      Unlike createBeanFromMethod(Class, Object, Predicate, Object...), no search is performed: the caller supplies the MethodInfo it has already chosen. Visibility is not a gate: public, protected, package-private and private methods are all invoked (non-public methods are made accessible first). Deprecation is not checked.

      If onClassOrObject is a Class, only static methods can be invoked. Otherwise the object is used as the receiver for instance methods.

      Returns Optional.empty() when the method is not eligible: it returns void, it is a bridge or synthetic method, it is an instance method and onClassOrObject is a Class, or one of its parameters cannot be resolved from this store plus extraBeans.

      Example:

      // Invoke one specific @Bean factory method, instead of searching by return type. MethodInfo m = ClassInfo.of(MyResource.class).getDeclaredMethod(x -> x.hasName("cacheIndicator")).get(); Optional<HealthIndicator> cache = beanStore.invokeBeanMethod(HealthIndicator.class, m, resource);

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The expected bean type. Must be assignable from the method's return type. Must not be null.
      method - The factory method to invoke. Must not be null.
      onClassOrObject - The receiver object, or the Class for static methods. Must not be null. The method's declaring class must be assignable from this object's class (or this class).
      extraBeans - Optional bean instances visible to parameter resolution for this call only.
      Returns:
      The created bean, or Optional.empty() if the method is not eligible.
      Throws:
      IllegalArgumentException - If an argument is null, beanType is not assignable from the method's return type, or the method does not belong to the target's class hierarchy.
      BeanCreationException - If the method cannot be made accessible or threw an exception during invocation.
    • createBeanFromMethod

      default <T> Optional<T> createBeanFromMethod(Class<T> beanType, Object onClassOrObject)
      Convenience overload of createBeanFromMethod(Class, Object, Predicate, Object...) with no filter and no extra beans.
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The type of bean to create. Must not be null.
      onClassOrObject - The object instance or Class whose methods are searched. Must not be null.
      Returns:
      The created bean wrapped in an Optional, or Optional.empty() if no matching factory method was found.
      Throws:
      BeanCreationException - If a matching method was found but threw an exception during invocation.
    • getBeanType

      default <T> Optional<Class<? extends T>> getBeanType(Class<T> beanType)
      Returns the implementation class registered for the specified bean type, if any.

      Type bindings let callers register a default implementation class to be instantiated when an actual bean of beanType is needed. Bindings are inherited through the parent chain.

      The default implementation returns Optional.empty() (no binding). Override in concrete stores (see BasicBeanStore) to enable type binding.

      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type whose implementation class is being looked up.
      Returns:
      The implementation class, or Optional.empty() if no binding exists.
    • hasAllParams

      default boolean hasAllParams(ExecutableInfo executable, Object outer)
      Returns true if this store can satisfy every parameter of the specified executable.

      Skips:

      • The first parameter when it is an instance of outer (inner-class outer reference).
      • Parameters typed as Optional (always considered satisfied).
      Parameters:
      executable - The constructor or method to inspect. Must not be null.
      outer - The outer object to use for inner-class param resolution. Can be null (the outer-instance skip rule is then never applied).
      Returns:
      true if every required parameter can be resolved from this store.
    • getParams

      default Object[] getParams(ExecutableInfo executable, Object outer)
      Resolves every parameter of the specified executable from this store.

      For Optional-typed parameters the wrapped Optional is returned directly; for all others the bean instance (or null when missing) is returned. See hasAllParams(org.apache.juneau.commons.reflect.ExecutableInfo, java.lang.Object) for the full skip rules.

      Parameters:
      executable - The constructor or method to resolve parameters for. Must not be null.
      outer - The outer object to use for inner-class param resolution. Can be null (the outer-instance skip rule is then never applied).
      Returns:
      An array of resolved parameter values, one per parameter of executable.
    • getMissingParams

      default String getMissingParams(ExecutableInfo executable, Object outer)
      Returns a comma-delimited list of parameter type names that cannot be resolved from this store.

      Intended for diagnostic error messages — see hasAllParams(org.apache.juneau.commons.reflect.ExecutableInfo, java.lang.Object) for the exact match rules. Skipped parameters (outer-instance match, Optional) never appear in the result.

      Parameters:
      executable - The constructor or method to inspect. Must not be null.
      outer - The outer object to use for inner-class param resolution. Can be null (the outer-instance skip rule is then never applied).
      Returns:
      A comma-delimited list of missing parameter type names, or null when none are missing.