Class DefaultPicoContainer

    • Field Detail

      • componentFactory

        protected final ComponentFactory componentFactory
        Component factory instance.
      • lifecycleStrategy

        protected final LifecycleStrategy lifecycleStrategy
        Lifecycle strategy instance.
      • componentMonitor

        protected ComponentMonitor componentMonitor
        Component monitor instance. Receives event callbacks.
    • Constructor Detail

      • DefaultPicoContainer

        public DefaultPicoContainer​(ComponentFactory componentFactory,
                                    PicoContainer parent)
        Creates a new container with a custom ComponentFactory and a parent container.

        Important note about caching: If you intend the components to be cached, you should pass in a factory that creates Cached instances, such as for example Caching. Caching can delegate to other ComponentAdapterFactories.

        Parameters:
        componentFactory - the factory to use for creation of ComponentAdapters.
        parent - the parent container (used for component dependency lookups).
      • DefaultPicoContainer

        public DefaultPicoContainer​(ComponentFactory componentFactory,
                                    LifecycleStrategy lifecycleStrategy,
                                    PicoContainer parent)
        Creates a new container with a custom ComponentFactory, LifecycleStrategy for instance registration, and a parent container.

        Important note about caching: If you intend the components to be cached, you should pass in a factory that creates Cached instances, such as for example Caching. Caching can delegate to other ComponentAdapterFactories.

        Parameters:
        componentFactory - the factory to use for creation of ComponentAdapters.
        lifecycleStrategy - the lifecycle strategy chosen for registered instance (not implementations!)
        parent - the parent container (used for component dependency lookups).
      • DefaultPicoContainer

        public DefaultPicoContainer​(ComponentMonitor monitor,
                                    PicoContainer parent)
        Creates a new container with the AdaptingInjection using a custom ComponentMonitor
        Parameters:
        monitor - the ComponentMonitor to use
        parent - the parent container (used for component dependency lookups).
      • DefaultPicoContainer

        public DefaultPicoContainer​(ComponentMonitor monitor,
                                    LifecycleStrategy lifecycleStrategy,
                                    PicoContainer parent)
        Creates a new container with the AdaptingInjection using a custom ComponentMonitor and lifecycle strategy
        Parameters:
        monitor - the ComponentMonitor to use
        lifecycleStrategy - the lifecycle strategy to use.
        parent - the parent container (used for component dependency lookups).
      • DefaultPicoContainer

        public DefaultPicoContainer​(LifecycleStrategy lifecycleStrategy,
                                    PicoContainer parent)
        Creates a new container with the AdaptingInjection using a custom lifecycle strategy
        Parameters:
        lifecycleStrategy - the lifecycle strategy to use.
        parent - the parent container (used for component dependency lookups).
      • DefaultPicoContainer

        public DefaultPicoContainer​(ComponentFactory componentFactory)
        Creates a new container with a custom ComponentFactory and no parent container.
        Parameters:
        componentFactory - the ComponentFactory to use.
      • DefaultPicoContainer

        public DefaultPicoContainer​(ComponentMonitor monitor)
        Creates a new container with the AdaptingInjection using a custom ComponentMonitor
        Parameters:
        monitor - the ComponentMonitor to use
      • DefaultPicoContainer

        public DefaultPicoContainer​(PicoContainer parent)
        Creates a new container with a (caching) AdaptingInjection and a parent container.
        Parameters:
        parent - the parent container (used for component dependency lookups).
      • DefaultPicoContainer

        public DefaultPicoContainer()
        Creates a new container with a AdaptingBehavior and no parent container.
    • Method Detail

      • getComponentAdapter

        public final ComponentAdapter<?> getComponentAdapter​(Object componentKey)
        Find a component adapter associated with the specified key. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.
        Specified by:
        getComponentAdapter in interface PicoContainer
        Parameters:
        componentKey - the key that the component was registered with.
        Returns:
        the component adapter associated with this key, or null if no component has been registered for the specified key.
      • getComponentAdapter

        public <T> ComponentAdapter<T> getComponentAdapter​(Class<T> componentType,
                                                           NameBinding componentNameBinding)
        Find a component adapter associated with the specified type and binding name. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.
        Specified by:
        getComponentAdapter in interface PicoContainer
        Parameters:
        componentType - the type of the component.
        componentNameBinding - the name binding to use
        Returns:
        the component adapter associated with this class, or null if no component has been registered for the specified key.
      • getComponentAdapter

        public <T> ComponentAdapter<T> getComponentAdapter​(Class<T> componentType,
                                                           Class<? extends Annotation> binding)
        Find a component adapter associated with the specified type and binding type. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.
        Specified by:
        getComponentAdapter in interface PicoContainer
        Parameters:
        componentType - the type of the component.
        binding - the typed binding to use
        Returns:
        the component adapter associated with this class, or null if no component has been registered for the specified key.
      • getComponentAdapters

        public <T> List<ComponentAdapter<T>> getComponentAdapters​(Class<T> componentType)
        Retrieve all component adapters inside this container that are associated with the specified type. The addComponent adapters from the parent container are not returned.
        Specified by:
        getComponentAdapters in interface PicoContainer
        Parameters:
        componentType - the type of the components.
        Returns:
        a collection containing all the ComponentAdapters inside this container that are associated with the specified type. Changes to this collection will not be reflected in the container itself.
      • getComponentAdapters

        public <T> List<ComponentAdapter<T>> getComponentAdapters​(Class<T> componentType,
                                                                  Class<? extends Annotation> binding)
        Retrieve all component adapters inside this container that are associated with the specified type and binding type. The addComponent adapters from the parent container are not returned.
        Specified by:
        getComponentAdapters in interface PicoContainer
        Parameters:
        componentType - the type of the components.
        binding - the typed binding to use
        Returns:
        a collection containing all the ComponentAdapters inside this container that are associated with the specified type. Changes to this collection will not be reflected in the container itself.
      • addAdapter

        public MutablePicoContainer addAdapter​(ComponentAdapter<?> componentAdapter)
        Register a component via a ComponentAdapter. Use this if you need fine grained control over what ComponentAdapter to use for a specific component. The adapter will be wrapped in whatever behaviors that the the container has been set up with. If you want to bypass that behavior for the adapter you are adding, you should use Characteristics.NONE like so pico.as(Characteristics.NONE).addAdapter(...) This method can be used to override the ComponentAdapter created by the ComponentFactory passed to the constructor of this container.
        Specified by:
        addAdapter in interface MutablePicoContainer
        Parameters:
        componentAdapter - the adapter
        Returns:
        the same instance of MutablePicoContainer
      • removeComponent

        public <T> ComponentAdapter<T> removeComponent​(Object componentKey)
        Unregister a component by key.
        Specified by:
        removeComponent in interface MutablePicoContainer
        Parameters:
        componentKey - key of the component to unregister.
        Returns:
        the ComponentAdapter that was associated with this component.
      • addComponent

        public MutablePicoContainer addComponent​(Object implOrInstance)
        Register an arbitrary object. The class of the object will be used as a key. Calling this method is equivalent to calling addComponent(componentImplementation, componentImplementation). The returned ComponentAdapter will be an InstanceAdapter.
        Specified by:
        addComponent in interface MutablePicoContainer
        Parameters:
        implOrInstance - Component implementation or instance
        Returns:
        the same instance of MutablePicoContainer
      • addComponent

        public MutablePicoContainer addComponent​(Object componentKey,
                                                 Object componentImplementationOrInstance,
                                                 Parameter... parameters)
        Register a component and creates specific instructions on which constructor to use, along with which components and/or constants to provide as constructor arguments. These "directives" are provided through an array of Parameter objects. Parameter[0] correspondes to the first constructor argument, Parameter[N] corresponds to the N+1th constructor argument.

        Tips for Parameter usage

        • Partial Autowiring: If you have two constructor args to match and you only wish to specify one of the constructors and let PicoContainer wire the other one, you can use as parameters: new ComponentParameter(), new ComponentParameter("someService") The default constructor for the component parameter indicates auto-wiring should take place for that parameter.
        • Force No-Arg constructor usage: If you wish to force a component to be constructed with the no-arg constructor, use a zero length Parameter array. Ex: new Parameter[0]
            The returned ComponentAdapter will be instantiated by the ComponentFactory passed to the container's constructor.
        Specified by:
        addComponent in interface MutablePicoContainer
        Parameters:
        componentKey - a key that identifies the component. Must be unique within the container. The type of the key object has no semantic significance unless explicitly specified in the documentation of the implementing container.
        componentImplementationOrInstance - the component's implementation class. This must be a concrete class (ie, a class that can be instantiated). Or an intance of the compoent.
        parameters - the parameters that gives the container hints about what arguments to pass to the constructor when it is instantiated. Container implementations may ignore one or more of these hints.
        Returns:
        the same instance of MutablePicoContainer
        See Also:
        Parameter, ConstantParameter, ComponentParameter
      • getComponents

        public List<Object> getComponents()
                                   throws PicoException
        Description copied from interface: PicoContainer
        Retrieve all the registered component instances in the container, (not including those in the parent container). The components are returned in their order of instantiation, which depends on the dependency order between them.
        Specified by:
        getComponents in interface PicoContainer
        Returns:
        all the components.
        Throws:
        PicoException - if the instantiation of the component fails
      • getComponents

        public <T> List<T> getComponents​(Class<T> componentType)
        Description copied from interface: PicoContainer
        Returns a List of components of a certain componentType. The list is ordered by instantiation order, starting with the components instantiated first at the beginning.
        Specified by:
        getComponents in interface PicoContainer
        Parameters:
        componentType - the searched type.
        Returns:
        a List of components.
      • getComponent

        public Object getComponent​(Object componentKeyOrType)
        Description copied from interface: PicoContainer
        Retrieve a component instance registered with a specific key or type. If a component cannot be found in this container, the parent container (if one exists) will be searched.
        Specified by:
        getComponent in interface PicoContainer
        Parameters:
        componentKeyOrType - the key or Type that the component was registered with.
        Returns:
        an instantiated component, or null if no component has been registered for the specified key.
      • decorateComponent

        protected Object decorateComponent​(Object component,
                                           ComponentAdapter<?> componentAdapter)
        This is invoked when getComponent(..) is called. It allows extendees to decorate a component before it is returned to the caller.
        Parameters:
        component - the component that will be returned for getComponent(..)
        componentAdapter - the component adapter that made that component
        Returns:
        the component (the same as that passed in by default)
      • getComponent

        public <T> T getComponent​(Class<T> componentType)
        Description copied from interface: PicoContainer
        Retrieve a component keyed by the component type.
        Specified by:
        getComponent in interface PicoContainer
        Parameters:
        componentType - the type of the component
        Returns:
        the typed resulting object instance or null if the object does not exist.
      • getComponent

        public <T> T getComponent​(Class<T> componentType,
                                  Class<? extends Annotation> binding)
        Description copied from interface: PicoContainer
        Retrieve a component keyed by the component type and binding type.
        Specified by:
        getComponent in interface PicoContainer
        Parameters:
        componentType - the type of the component
        binding - the binding type of the component
        Returns:
        the typed resulting object instance or null if the object does not exist.
      • removeComponentByInstance

        public <T> ComponentAdapter<T> removeComponentByInstance​(T componentInstance)
        Unregister a component by instance.
        Specified by:
        removeComponentByInstance in interface MutablePicoContainer
        Parameters:
        componentInstance - the component instance to unregister.
        Returns:
        the same instance of MutablePicoContainer
      • setLifecycleState

        public void setLifecycleState​(LifecycleState lifecycleState)
        Description copied from interface: MutablePicoContainer
        To assist ThreadLocal usage, LifecycleState can be set. No need to use this for normal usages.
        Specified by:
        setLifecycleState in interface MutablePicoContainer
        Parameters:
        lifecycleState - the lifecyle state to use.
      • makeChildContainer

        public MutablePicoContainer makeChildContainer()
        Description copied from interface: MutablePicoContainer
        Make a child container, using both the same implementation of MutablePicoContainer as the parent and identical behaviors as well. It will have a reference to this as parent. This will list the resulting MPC as a child. Lifecycle events will be cascaded from parent to child as a consequence of this.

        Note that for long-lived parent containers, you need to unregister child containers made with this call before disposing or you will leak memory. (Experience speaking here! )

        Incorrect Example:

           MutablePicoContainer parent = new PicoBuilder().withCaching().withLifecycle().build();
           MutablePicoContainer child = parent.makeChildContainer();
           child = null; //Child still retains in memory because parent still holds reference.
         

        Correct Example:

           MutablePicoContainer parent = new PicoBuilder().withCaching().withLifecycle().build();
           MutablePicoContainer child = parent.makeChildContainer();
           parent.removeChildContainer(child); //Remove the bi-directional references.
           child = null; 
         
        Specified by:
        makeChildContainer in interface MutablePicoContainer
        Returns:
        the new child container.
      • addChildContainer

        public MutablePicoContainer addChildContainer​(PicoContainer child)
        Description copied from interface: MutablePicoContainer
        Add a child container. This action will list the the 'child' as exactly that in the parents scope. It will not change the child's view of a parent. That is determined by the constructor arguments of the child itself. Lifecycle events will be cascaded from parent to child as a consequence of calling this method.
        Specified by:
        addChildContainer in interface MutablePicoContainer
        Parameters:
        child - the child container
        Returns:
        the same instance of MutablePicoContainer
      • removeChildContainer

        public boolean removeChildContainer​(PicoContainer child)
        Description copied from interface: MutablePicoContainer
        Remove a child container from this container. It will not change the child's view of a parent. Lifecycle event will no longer be cascaded from the parent to the child.
        Specified by:
        removeChildContainer in interface MutablePicoContainer
        Parameters:
        child - the child container
        Returns:
        true if the child container has been removed.
      • accept

        public void accept​(PicoVisitor visitor)
        Description copied from interface: PicoContainer
        Accepts a visitor that should visit the child containers, component adapters and component instances.
        Specified by:
        accept in interface PicoContainer
        Parameters:
        visitor - the visitor
      • changeMonitor

        public void changeMonitor​(ComponentMonitor monitor)
        Changes monitor in the ComponentFactory, the component adapters and the child containers, if these support a ComponentMonitorStrategy. Changes the component monitor used
        Specified by:
        changeMonitor in interface ComponentMonitorStrategy
        Parameters:
        monitor - the new ComponentMonitor to use
      • currentMonitor

        public ComponentMonitor currentMonitor()
        Returns the first current monitor found in the ComponentFactory, the component adapters and the child containers, if these support a ComponentMonitorStrategy. Returns the monitor currently used
        Specified by:
        currentMonitor in interface ComponentMonitorStrategy
        Returns:
        The ComponentMonitor currently used
        Throws:
        PicoCompositionException - if no component monitor is found in container or its children
      • potentiallyStartAdapter

        protected void potentiallyStartAdapter​(ComponentAdapter<?> adapter)
      • instantiateComponentAsIsStartable

        protected void instantiateComponentAsIsStartable​(ComponentAdapter<?> adapter)
      • getOrderedComponentAdapters

        protected List<ComponentAdapter<?>> getOrderedComponentAdapters()
        Returns:
        the orderedComponentAdapters
      • getComponentKeyToAdapterCache

        protected Map<Object,​ComponentAdapter<?>> getComponentKeyToAdapterCache()
        Returns:
        the componentKeyToAdapterCache
      • getModifiableComponentAdapterList

        protected Set<ComponentAdapter<?>> getModifiableComponentAdapterList()
        Returns:
        the componentAdapters
      • setName

        public void setName​(String name)
        Description copied from interface: MutablePicoContainer
        Name the container instance, to assist debugging or other indexing.
        Specified by:
        setName in interface MutablePicoContainer
        Parameters:
        name - the name to call it.
      • getConverters

        public Converters getConverters()
        If this container has a set of converters, then return it. If it does not, and the parent (or their parent ..) does, use that If they do not, return a NullObject implementation (ConversNothing)
        Specified by:
        getConverters in interface Converting
        Returns:
        the converters