Package org.picocontainer
Class DefaultPicoContainer
java.lang.Object
org.picocontainer.DefaultPicoContainer
- All Implemented Interfaces:
Serializable
,ComponentMonitorStrategy
,Converting
,Disposable
,MutablePicoContainer
,PicoContainer
,Startable
- Direct Known Subclasses:
TieringPicoContainer
,TransientPicoContainer
public class DefaultPicoContainer
extends Object
implements MutablePicoContainer, Converting, ComponentMonitorStrategy, Serializable
The Standard
PicoContainer
/MutablePicoContainer
implementation.
Constructing a container c with a parent p container will cause c to look up components
in p if they cannot be found inside c itself.
Using Class
objects as keys to the various registerXXX() methods makes
a subtle semantic difference:
If there are more than one registered components of the same type and one of them are
registered with a Class
key of the corresponding type, this addComponent
will take precedence over other components during type resolution.
Another place where keys that are classes make a subtle difference is in
HiddenImplementation
.
This implementation of MutablePicoContainer
also supports
ComponentMonitorStrategy
.
- Author:
- Paul Hammant, Aslak Hellesøy, Jon Tirsén, Thomas Heller, Mauro Talevi
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ComponentFactory
Component factory instance.protected ComponentMonitor
Component monitor instance.protected final LifecycleStrategy
Lifecycle strategy instance.protected final List<ComponentAdapter<?>>
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new container with aAdaptingBehavior
and no parent container.DefaultPicoContainer
(ComponentFactory componentFactory) Creates a new container with a custom ComponentFactory and no parent container.DefaultPicoContainer
(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent) Creates a new container with a custom ComponentFactory, LifecycleStrategy for instance registration, and a parent container.DefaultPicoContainer
(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, ComponentMonitor componentMonitor) DefaultPicoContainer
(ComponentFactory componentFactory, PicoContainer parent) Creates a new container with a custom ComponentFactory and a parent container.DefaultPicoContainer
(ComponentMonitor monitor) Creates a new container with the AdaptingInjection using a custom ComponentMonitorDefaultPicoContainer
(ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, PicoContainer parent) Creates a new container with the AdaptingInjection using a custom ComponentMonitor and lifecycle strategyDefaultPicoContainer
(ComponentMonitor monitor, PicoContainer parent) Creates a new container with the AdaptingInjection using a custom ComponentMonitorDefaultPicoContainer
(LifecycleStrategy lifecycleStrategy, PicoContainer parent) Creates a new container with the AdaptingInjection using a custom lifecycle strategyDefaultPicoContainer
(PicoContainer parent) Creates a new container with a (caching)AdaptingInjection
and a parent container. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(PicoVisitor visitor) Accepts a visitor that should visit the child containers, component adapters and component instances.addAdapter
(ComponentAdapter<?> componentAdapter) Register a component via a ComponentAdapter.addAdapter
(ComponentAdapter<?> componentAdapter, Properties properties) protected MutablePicoContainer
addAdapterInternal
(ComponentAdapter<?> componentAdapter) addChildContainer
(PicoContainer child) Add a child container.addComponent
(Object implOrInstance) Register an arbitrary object.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.Register a config item.as
(Properties... properties) You can set for the following operation only the characteristic of registration of a component on the fly.change
(Properties... properties) You can change the characteristic of registration of all subsequent components in this container.void
changeMonitor
(ComponentMonitor monitor) Changes monitor in the ComponentFactory, the component adapters and the child containers, if these support a ComponentMonitorStrategy.Returns the first current monitor found in the ComponentFactory, the component adapters and the child containers, if these support a ComponentMonitorStrategy.protected Object
decorateComponent
(Object component, ComponentAdapter<?> componentAdapter) This is invoked when getComponent(..) is called.void
dispose()
Dispose the components of this PicoContainer and all its logical child containers.<T> T
getComponent
(Class<T> componentType) Retrieve a component keyed by the component type.<T> T
getComponent
(Class<T> componentType, Class<? extends Annotation> binding) Retrieve a component keyed by the component type and binding type.getComponent
(Object componentKeyOrType) Retrieve a component instance registered with a specific key or type.getComponent
(Object componentKeyOrType, Class<? extends Annotation> annotation) getComponent
(Object componentKeyOrType, Type into) <T> ComponentAdapter<T>
getComponentAdapter
(Class<T> componentType, Class<? extends Annotation> binding) Find a component adapter associated with the specified type and binding type.<T> ComponentAdapter<T>
getComponentAdapter
(Class<T> componentType, NameBinding componentNameBinding) Find a component adapter associated with the specified type and binding name.final ComponentAdapter<?>
getComponentAdapter
(Object componentKey) Find a component adapter associated with the specified key.Retrieve all the component adapters inside this container.<T> List<ComponentAdapter<T>>
getComponentAdapters
(Class<T> componentType) Retrieve all component adapters inside this container that are associated with the specified type.<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.protected Map<Object,
ComponentAdapter<?>> Retrieve all the registered component instances in the container, (not including those in the parent container).<T> List<T>
getComponents
(Class<T> componentType) Returns a List of components of a certain componentType.If this container has a set of converters, then return it.Allow querying of the current lifecycle state of a MutablePicoContainer.protected Set<ComponentAdapter<?>>
getName()
Retrieve the name set (if any).protected List<ComponentAdapter<?>>
Retrieve the parent container of this container.protected void
instantiateComponentAsIsStartable
(ComponentAdapter<?> adapter) Make a child container, using both the same implementation of MutablePicoContainer as the parent and identical behaviors as well.protected void
potentiallyStartAdapter
(ComponentAdapter<?> adapter) boolean
Remove a child container from this container.<T> ComponentAdapter<T>
removeComponent
(Object componentKey) Unregister a component by key.<T> ComponentAdapter<T>
removeComponentByInstance
(T componentInstance) Unregister a component by instance.void
setLifecycleState
(LifecycleState lifecycleState) To assist ThreadLocal usage, LifecycleState can be set.void
Name the container instance, to assist debugging or other indexing.void
start()
Start the components of this PicoContainer and all its logical child containers.void
stop()
Stop the components of this PicoContainer and all its logical child containers.toString()
-
Field Details
-
componentFactory
Component factory instance. -
lifecycleStrategy
Lifecycle strategy instance. -
componentMonitor
Component monitor instance. Receives event callbacks. -
orderedComponentAdapters
-
-
Constructor Details
-
DefaultPicoContainer
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 createsCached
instances, such as for exampleCaching
. 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 createsCached
instances, such as for exampleCaching
. 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(ComponentFactory componentFactory, LifecycleStrategy lifecycleStrategy, PicoContainer parent, ComponentMonitor componentMonitor) -
DefaultPicoContainer
Creates a new container with the AdaptingInjection using a custom ComponentMonitor- Parameters:
monitor
- the ComponentMonitor to useparent
- 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 uselifecycleStrategy
- the lifecycle strategy to use.parent
- the parent container (used for component dependency lookups).
-
DefaultPicoContainer
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
Creates a new container with a custom ComponentFactory and no parent container.- Parameters:
componentFactory
- the ComponentFactory to use.
-
DefaultPicoContainer
Creates a new container with the AdaptingInjection using a custom ComponentMonitor- Parameters:
monitor
- the ComponentMonitor to use
-
DefaultPicoContainer
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 aAdaptingBehavior
and no parent container.
-
-
Method Details
-
getComponentAdapters
Retrieve all the component adapters inside this container. The component adapters from the parent container are not returned.- Specified by:
getComponentAdapters
in interfacePicoContainer
- Returns:
- a collection containing all the
ComponentAdapter
s inside this container. The collection will not be modifiable. - See Also:
-
getComponentAdapter
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 interfacePicoContainer
- 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 interfacePicoContainer
- 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 interfacePicoContainer
- 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
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 interfacePicoContainer
- Parameters:
componentType
- the type of the components.- Returns:
- a collection containing all the
ComponentAdapter
s 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 interfacePicoContainer
- Parameters:
componentType
- the type of the components.binding
- the typed binding to use- Returns:
- a collection containing all the
ComponentAdapter
s inside this container that are associated with the specified type. Changes to this collection will not be reflected in the container itself.
-
addAdapterInternal
-
addAdapter
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 theComponentFactory
passed to the constructor of this container.- Specified by:
addAdapter
in interfaceMutablePicoContainer
- Parameters:
componentAdapter
- the adapter- Returns:
- the same instance of MutablePicoContainer
-
addAdapter
-
removeComponent
Unregister a component by key.- Specified by:
removeComponent
in interfaceMutablePicoContainer
- Parameters:
componentKey
- key of the component to unregister.- Returns:
- the ComponentAdapter that was associated with this component.
-
addComponent
Register an arbitrary object. The class of the object will be used as a key. Calling this method is equivalent to callingaddComponent(componentImplementation, componentImplementation)
. The returned ComponentAdapter will be anInstanceAdapter
.- Specified by:
addComponent
in interfaceMutablePicoContainer
- Parameters:
implOrInstance
- Component implementation or instance- Returns:
- the same instance of MutablePicoContainer
-
addConfig
Description copied from interface:MutablePicoContainer
Register a config item.- Specified by:
addConfig
in interfaceMutablePicoContainer
- Parameters:
name
- the name of the config itemval
- the value of the config item- 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 interfaceMutablePicoContainer
- 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:
- 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:
-
getComponents
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 interfacePicoContainer
- Returns:
- all the components.
- Throws:
PicoException
- if the instantiation of the component fails
-
getComponents
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 interfacePicoContainer
- Parameters:
componentType
- the searched type.- Returns:
- a List of components.
-
getComponent
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 interfacePicoContainer
- 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.
-
getComponent
- Specified by:
getComponent
in interfacePicoContainer
-
getComponent
-
decorateComponent
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
Description copied from interface:PicoContainer
Retrieve a component keyed by the component type.- Specified by:
getComponent
in interfacePicoContainer
- Parameters:
componentType
- the type of the component- Returns:
- the typed resulting object instance or null if the object does not exist.
-
getComponent
Description copied from interface:PicoContainer
Retrieve a component keyed by the component type and binding type.- Specified by:
getComponent
in interfacePicoContainer
- Parameters:
componentType
- the type of the componentbinding
- the binding type of the component- Returns:
- the typed resulting object instance or null if the object does not exist.
-
getParent
Retrieve the parent container of this container.- Specified by:
getParent
in interfacePicoContainer
- Returns:
- a
PicoContainer
instance, ornull
if this container does not have a parent.
-
removeComponentByInstance
Unregister a component by instance.- Specified by:
removeComponentByInstance
in interfaceMutablePicoContainer
- Parameters:
componentInstance
- the component instance to unregister.- Returns:
- the same instance of MutablePicoContainer
-
start
public void start()Start the components of this PicoContainer and all its logical child containers. The starting of the child container is only attempted if the parent container start successfully. The child container for which start is attempted is tracked so that upon stop, only those need to be stopped. The lifecycle operation is delegated to the component adapter, if it is an instance oflifecycle manager
. The actuallifecycle strategy
supported depends on the concrete implementation of the adapter. -
stop
public void stop()Stop the components of this PicoContainer and all its logical child containers. The stopping of the child containers is only attempted for those that have been started, possibly not successfully. The lifecycle operation is delegated to the component adapter, if it is an instance oflifecycle manager
. The actuallifecycle strategy
supported depends on the concrete implementation of the adapter. -
dispose
public void dispose()Dispose the components of this PicoContainer and all its logical child containers. The lifecycle operation is delegated to the component adapter, if it is an instance oflifecycle manager
. The actuallifecycle strategy
supported depends on the concrete implementation of the adapter.- Specified by:
dispose
in interfaceDisposable
- See Also:
-
setLifecycleState
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 interfaceMutablePicoContainer
- Parameters:
lifecycleState
- the lifecyle state to use.
-
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 interfaceMutablePicoContainer
- Returns:
- the new child container.
-
addChildContainer
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 interfaceMutablePicoContainer
- Parameters:
child
- the child container- Returns:
- the same instance of MutablePicoContainer
-
removeChildContainer
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 interfaceMutablePicoContainer
- Parameters:
child
- the child container- Returns:
true
if the child container has been removed.
-
change
Description copied from interface:MutablePicoContainer
You can change the characteristic of registration of all subsequent components in this container.- Specified by:
change
in interfaceMutablePicoContainer
- Returns:
- the same Pico instance with changed properties
-
as
Description copied from interface:MutablePicoContainer
You can set for the following operation only the characteristic of registration of a component on the fly.- Specified by:
as
in interfaceMutablePicoContainer
- Returns:
- the same Pico instance with temporary properties
-
accept
Description copied from interface:PicoContainer
Accepts a visitor that should visit the child containers, component adapters and component instances.- Specified by:
accept
in interfacePicoContainer
- Parameters:
visitor
- the visitor
-
changeMonitor
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 interfaceComponentMonitorStrategy
- Parameters:
monitor
- the new ComponentMonitor to use
-
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 interfaceComponentMonitorStrategy
- Returns:
- The ComponentMonitor currently used
- Throws:
PicoCompositionException
- if no component monitor is found in container or its children
-
potentiallyStartAdapter
-
instantiateComponentAsIsStartable
-
getOrderedComponentAdapters
- Returns:
- the orderedComponentAdapters
-
getComponentKeyToAdapterCache
- Returns:
- the componentKeyToAdapterCache
-
getModifiableComponentAdapterList
- Returns:
- the componentAdapters
-
setName
Description copied from interface:MutablePicoContainer
Name the container instance, to assist debugging or other indexing.- Specified by:
setName
in interfaceMutablePicoContainer
- Parameters:
name
- the name to call it.
-
toString
-
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 interfaceConverting
- Returns:
- the converters
-
getLifecycleState
Allow querying of the current lifecycle state of a MutablePicoContainer.- Specified by:
getLifecycleState
in interfaceMutablePicoContainer
- Returns:
- the current Lifecycle State.
- See Also:
-
getName
Retrieve the name set (if any).- Specified by:
getName
in interfaceMutablePicoContainer
- Returns:
- Retrieve the arbitrary name of the container set by calling
setName
. - See Also:
-