PicoContainer - How to use primitive types in constructors

Authors: Jörg Schaible

PicoContainer will look for the greediest constructor of your component. But if your component's constructor depends on primitive types you may set the values explicitly.

public interface ThreadPool {
	void setSize(int);
}

public class MyComp {
	private ThreadPool threadPool;
	public MyComp(ThreadPool pool, int size) {
		threadPool = pool;
		threadPool.setSize(size);
	}
}

In this case you can set the parameters at registration time:

DefaultPicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(ThreadPool.class, DefaultThreadPool.class);
pico.registerComponentImplementation(MyComp.class, MyComp.class, new Parameters[] {
	new ComponentParameter(),
	new ConstantParameter(new Integer(5));
})
MyComp myComp = (MyComp)pico.getInstance(MyComp.class);

Use ConstantParameter to set constant values and the ComponentParameter to let Pico resolve the dependency.