Two minute tutorial

Authors: Jon Tirsen

This very short tutorial should get you up to speed with PicoContainer in 2 minutes. It does not go into why you should do it, read the Five minute introduction for that.

Download and install

Downloads the jar file and include it in your classpath.

Write two simple components

public class Boy {
    public void kiss(Object kisser) {
        System.out.println("I was kissed by " + kisser);
    }
}

 

public class Girl {
    Boy boy;

    public Girl(Boy boy) {
        this.boy = boy;
    }

    public void kissSomeone() {
        boy.kiss(this);
    }
}

 

Assemble components

MutablePicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(Boy.class);
pico.registerComponentImplementation(Girl.class);

 

MutablePicoContainer API

Instantiate and use component

Girl girl = (Girl) pico.getComponentInstance(Girl.class);
girl.kissSomeone();

 

getComponentInstance will look at the Girl class and determine that it needs to create a Boy instance and pass that into the constructor to create a Girl. The Boy is created and then the Girl.

PicoContainer API

The Girl does not reach out to find herself a Boy but instead is provided one by the container. This is called the Hollywood Principle or "Don't call us we'll call you".

Introduce an interface for the dependency

Change the Boy class to implement a Kissable interface and change the Girl class to depend on Kissable instead.

public interface Kissable {
    void kiss(Object kisser);
}

 

public class Boy implements Kissable {
    public void kiss(Object kisser) {
        System.out.println("I was kissed by " + kisser);
    }
}

 

public class Girl {
    Kissable kissable;

    public Girl(Kissable kissable) {
        this.kissable = kissable;
    }

    public void kissSomeone() {
        kissable.kiss(this);
    }
}

 

Assemble and use components just as before:

MutablePicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(Boy.class);
pico.registerComponentImplementation(Girl.class);

 

Or the preferred way:

MutablePicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(Kissable.class, Boy.class);
pico.registerComponentImplementation(Girl.class);

 

Now run:

Girl girl = (Girl) pico.getComponentInstance(Girl.class);
girl.kissSomeone();

 

The Girl will be given a Boy, because PicoContainer understands that it is a Kissable

The Girl and the Boy no longer depend on each other, this is called the Dependency Inversion Principle since both components depend on the interface and no longer directly on each other.

Use simple lifecycle

Change the Girl class to implement the simple default lifecycle and do it's kissing when the container is started.

public class Girl implements Startable {
    Kissable kissable;

    public Girl(Kissable kissable) {
        this.kissable = kissable;
    }

    public void start() {
        kissable.kiss(this);
    }

    public void stop() {
    }
}

 

Assemble container as before but instead of calling the Girl directly just start the container like this:

pico.start();

 

This will instantiate all components that implement Startable and call the start method on each of them. To stop and dispose the container do as follows:

pico.stop();
pico.dispose();

 

(i)Startable API

(i)Disposable API

More Quick Facts

  • PicoContainer can do Setter Depenedency Injection (SDI) via alternate ComponentAdapterFactories
  • PicoContainer cannot do registration by class name. Make some (tiny) code to do that or use NanoContainer in conjunction with PicoContainer
  • PicoContainer can has a pluggable design for Lifecycle - very flexible.
  • PicoContainer really likes to see components registered by type (interface) rather directly as implementations.

Next: Five minute introduction

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 01, 2005

    Archimedes Trajano says:

    What does "PicoContainer cannot do registration by class name" mean? Don't we a...

    What does "PicoContainer cannot do registration by class name" mean? Don't we already register by classes? pico.registerComponentImplementation(Girl.class);