Class BeanBag

java.lang.Object
io.smallrye.beanbag.BeanBag

public final class BeanBag extends Object
A basic bean container.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    A builder for an individual bean's configuration.
    static final class 
    A builder for a new container.
    static final class 
    A builder for a bean supplier which constructs a bean using reflection.
  • Method Summary

    Modifier and Type
    Method
    Description
    Construct a new container builder.
    <T> List<T>
    getAllBeans(Class<T> type)
    Get all constructable beans of the given type from a new resolution scope.
    <T> T
    Get a single bean with the given type from a new resolution scope, if it exists and can be instantiated.
    <T> T
    getOptionalBean(Class<T> type, String name)
    Get a single bean with the given type and name from a new resolution scope, if it exists and can be instantiated.
    Create a new resolution scope.
    <T> T
    requireBean(Class<T> type)
    Require a single bean with the given type from a new resolution scope.
    <T> T
    requireBean(Class<T> type, String name)
    Require a single bean with the given type and name from a new resolution scope.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • newScope

      public Scope newScope()
      Create a new resolution scope. A resolution scope maintains independent instances of its beans.
      Returns:
      the new resolution scope (not null)
    • getAllBeans

      public <T> List<T> getAllBeans(Class<T> type)
      Get all constructable beans of the given type from a new resolution scope.
      Type Parameters:
      T - the allowed bean type
      Parameters:
      type - the allowed bean type class (must not be null)
      Returns:
      the (possibly empty) list of all matching beans
    • requireBean

      public <T> T requireBean(Class<T> type)
      Require a single bean with the given type from a new resolution scope.
      Type Parameters:
      T - the allowed bean type
      Parameters:
      type - the allowed bean type class (must not be null)
      Returns:
      the single bean (not null)
      Throws:
      NoSuchBeanException - if the bean is not present
      BeanInstantiationException - if some error occurred when instantiating the bean
    • requireBean

      public <T> T requireBean(Class<T> type, String name)
      Require a single bean with the given type and name from a new resolution scope.
      Type Parameters:
      T - the allowed bean type
      Parameters:
      type - the allowed bean type class (must not be null)
      name - the name of the bean which should be returned, or "" for any (must not be null)
      Returns:
      the single bean (not null)
      Throws:
      NoSuchBeanException - if the bean is not present
      BeanInstantiationException - if some error occurred when instantiating the bean
    • getOptionalBean

      public <T> T getOptionalBean(Class<T> type)
      Get a single bean with the given type from a new resolution scope, if it exists and can be instantiated.
      Type Parameters:
      T - the allowed bean type
      Parameters:
      type - the allowed bean type class (must not be null)
      Returns:
      the single bean, or null if it is not present
    • getOptionalBean

      public <T> T getOptionalBean(Class<T> type, String name)
      Get a single bean with the given type and name from a new resolution scope, if it exists and can be instantiated.
      Type Parameters:
      T - the allowed bean type
      Parameters:
      type - the allowed bean type class (must not be null)
      name - the name of the bean which should be returned, or "" for any (must not be null)
      Returns:
      the single bean, or null if it is not present
    • builder

      public static BeanBag.Builder builder()
      Construct a new container builder.
      Returns:
      the new builder (not null)