Rather than new up a PicoContainer instance with the right ComponentFactory, LifecycleStategy and ComponentMonitor, your can use PicoBuilder for conveniece. Refer to Martin's 'Fluent Interface' article, for some of the motivation behing this class.
Some simple examples :
pico = new PicoBuilder().build(); pico.addComponent(Apple.class);
pico = new PicoBuilder().withCaching().build(); pico.addComponent(Apple.class);
pico = new PicoBuilder().withCaching().build(); pico.addComponent(Apple.class);
pico = new PicoBuilder().withHiddenImplementations().build(); pico.addComponent(Apple.class);
More varations for behaviors :
pico = new PicoBuilder().withLifecycle().withConsoleMonitor().build(); pico.addComponent(Apple.class);
pico = new PicoBuilder().withMonitor(ConsoleComponentMonitor.class).build(); pico.addComponent(Apple.class);
import static org.picocontainer.injectors.Injectors.SDI; import static org.picocontainer.behaviors.Behaviors.caching; import static org.picocontainer.behaviors.Behaviors.implementationHiding; import static org.picocontainer.behaviors.Behaviors.synchronizing; ... pico = new PicoBuilder(SDI()).withBehaviors(caching(), implementationHiding(), synchronizing()).build(); pico.addComponent(Apple.class);
Setting a parent container :
child = new PicoBuilder(parentContainer).build(); child.addComponent(Apple.class);
Specifying an injection type:
pico = new PicoBuilder().withAnnotationInjection().build(); pico.addComponent(Apple.class);
Specifying an injection type a different way:
import static org.picocontainer.injectors.Injectors.SDI; ... pico = new PicoBuilder(SDI()).build(); pico.addComponent(Apple.class);
A custom container component used by a custom ComponentBehavior (though could be used by ComponentMonitor or LifecycleStrategy as easily):
pico = new PicoBuilder() .withCustomContainerComponent(new MyQuantumPhysicsConnector()) .withComponentFactory(MyQuantumPhysicsConnectionNeedingComponentBehavior.class).build(); pico.addComponent(Apple.class);
A custom container implementation (DefaultPicoContainer is the default):
MyCustomPicoContainer pico = (MyCustomPicoContainer) new PicoBuilder().implementedBy(MyCustomPicoContainer.class).build(); pico.addComponent(Apple.class);