|
|||||
|
|||||
Two minute tutorial
This is a simple tutorial to help get a general understanding of how to use NanoContainer. The example used here will keep in context with the example given in the PicoContainer - Two minute tutorial . This should help show how NanoContainer is a natural extension to PicoContainer. Sample ComponentsAn error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/tutorial/simple/Boy.java. The system administrator has been notified. An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/tutorial/simple/Girl.java. The system administrator has been notified.Common codeThe following method will be utilized in each of the examples listed below. The buildContainer() method is responsible for reading the NanoContainer script (i.e. xml, groovy, js, etc...). It is responsible for building a PicoContainer from any ScriptedContainerBuilder object. public PicoContainer buildContainer(ScriptedContainerBuilder builder, PicoContainer parentContainer, Object scope) { ObjectReference containerRef = new SimpleReference(); ObjectReference parentContainerRef = new SimpleReference(); parentContainerRef.set(parentContainer); builder.buildContainer(containerRef, parentContainerRef, scope, true); return (PicoContainer) containerRef.get(); } Simple XML exampleSuppose we have a nanocontainer.xml file defined as the following: <container> <component-implementation class='Boy'/> <component-implementation class='Girl'/> </container> We need to read the script in and process it to build the container. import org.nanocontainer.script.xml.XMLContainerBuilder; ... Reader script = new FileReader("nanocontainer.xml"); XMLContainerBuilder builder = new XMLContainerBuilder(script, getClass().getClassLoader()); PicoContainer pico = buildContainer(builder, null, "SOME_SCOPE"); // Now instantiate and use the component Girl girl = (Girl) pico.getComponentInstance(Girl.class); girl.kissSomeone(); Simple Groovy exampleExample nanocontainer.groovy file: builder = new org.nanocontainer.script.groovy.NanoContainerBuilder()
nano = builder.container {
component(class:Girl)
component(class:Boy)
}
Now lets read in the groovy script and build the container. Notice that the only difference between this example and the previous XML example is the ScriptedContainerBuilder type (XMLContainerBuilder vs. GroovyContainerBuilder). import org.nanocontainer.script.groovy.GroovyContainerBuilder; ... Reader script = new FileReader("nanocontainer.groovy"); GroovyContainerBuilder builder = new GroovyContainerBuilder(script, getClass().getClassLoader()); PicoContainer pico = buildContainer(builder, null, "SOME_SCOPE"); // Now instantiate and use the component Girl girl = (Girl) pico.getComponentInstance(Girl.class); girl.kissSomeone();
|
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||