User Documentation

One minute description
Two minute tutorial
Five minute introduction
Advanced Topics
FAQ
Container
Components
Terminology
Mock Objects
Inversion of Control Types

Patterns

Inversion of Control 
Dependency Injection 
Constructor Injection 
Setter Injection 
Interface Implementation Separation 
Lifecycle 
Antipatterns

Developer Documentation

 Current build status
How To Contribute
Relative Volunteering
Release Process

Project Information

Slogan
Mailing lists
Source Repositories
Open Issues
Blog entries 
Statistics
Team
Sister Projects
TShirts

Miscellaneous

Differentiators
Nirvana

Full Sitemap

Feeds


Site
News
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.