Class BeanBag


  • public final class BeanBag
    extends Object
    A basic bean container.
    • Method Detail

      • 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)