User DocumentationOne minute descriptionTwo minute tutorial Five minute introduction Advanced Topics FAQ Container Components Terminology Mock Objects Inversion of Control Types PatternsInversion of ControlDependency Injection Constructor Injection Setter Injection Interface-Impl Separation Lifecycle Antipatterns Developer DocumentationHow To ContributeRelative Volunteering Release Process Project InformationSloganMailing lists Source Repositories Open Issues Blog entries Statistics Team Sister Projects TShirts MiscellaneousDifferentiatorsNirvana Full Sitemap |
Two minute tutorialAuthors: 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 installDownload the jar file and include it in your classpath.Write two simple componentspublic 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 componentsMutablePicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(Boy.class);
pico.registerComponentImplementation(Girl.class); ![]() Instantiate and use componentGirl girl = (Girl) pico.getComponentInstance(Girl.class); girl.kissSomeone(); ![]() Use interfaceChange 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. ![]() Use simple lifecycleChange 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(); ![]() ![]() Next: Five minute introduction TODOs for this document |