Inversion of Control is three things: component dependencies and configuration, but also Component Lifecycle.
After instantiation, if the component warrants it, a 'start' stage may be required. More specifically, if a container has injected and instantiated all components in a set, one or more of them may require starting in the same order they were instantiated. Later, in reverse order, the same component(s) may require stopping. In fact start and stop may happen more than one for the life of an application. Disposal may happen once only, before the component is eligible for garbage collection.
Lifecycle is really only going to work for PicoContainers that are also caching component instances. Caching was a default in PicoContainer 1.x, but is not for 2.x - be warned!
Thus lifecycle implies three methods:
In PicoContainer we think allow a pluggable LifecycleStrategy.
Our own interface for startable. We wish it were in the JDK, because we're big into making components unencumbered by the trappings of containment. In English: we'd rather not make components implement/extend/throw anything from our framework. Its a 'transparency' thing.
Here's an example of components fitting that ideal
public class Apple implements Startable{ public void start() { // listen on socket, start thread etc. } public void stop() { // stop listening on socket, kill thread etc. } } ... pico = new DefaultPicoContainer(new StartableLifecycleStrategy()); pico.addComponent(Apple.class); pico.start(); // start gets called Apple a = pico.getComponent(Apple.class);
The StartableLifecycleStrategy can be extended if you prefer your own interface for Startable. Just override
This works without an interface. Instead it works via reflection, and appropriate method names.
public class Apple { public void start() { // listen on socket, start thread etc. } public void stop() { // stop listening on socket, kill thread etc. } } ... pico = new DefaultPicoContainer(new ReflectionLifecycleStrategy()); pico.addComponent(Apple.class); pico.start(); // start gets called Apple a = pico.getComponent(Apple.class);
If you have other synonyms for start/stop/dispose, just extend the ReflectionLifecycleStrategy class and provide them.
This works without an interface, but with annotations in front of the designated methods
public class Apple { @PostConstruct public void startUp() { // listen on socket, start thread etc. } @PreDestroy public void allOver() { // stop listening on socket, kill thread etc. } } ... pico = new DefaultPicoContainer(new JavaEE5LifecycleStrategy()); pico.addComponent(Apple.class); pico.start(); // start gets called Apple a = pico.getComponent(Apple.class);
These annotations are supplied with Java 6, but come in a jar for Java 5 and below. See http://mvnrepository.com/artifact/javax.annotation/jsr250-api
DefaultPicoContainer does sets StartableLifecycleStrategy by default. You can specify NullLifecycleStrategy instead if you are sure that no components honor any lifecycle concept..
Write a class that implements LifecycleStragegy, there are just four methods to implement. See LifecycleStrategy.