|
|||||
|
|||||
Container
NanoContainer OverviewNanoContainer is a script-enabled layer which builds PicoContainers. Trees of containers and components can be composed using several scripting languages: Why multiple script language support?Multiple script languages are supported because we officially stand against a single meta-data language. Of the script languages, XML is the most widely used and mature, but Groovy is most promising because of the its optional builder syntax. Uses of NanoContainerNanoContainer is primarily intended to be embedded inside other applications and frameworks. We recommend using NanoContainer when your application consists of many different components that are related to each other but you need composition driven by external scripts. Embedded Example// Instantiate the reader. It should point to a script. FileReader script = ...; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // build the container from the script. GroovyContainerBuilder is used in this example // but the appropriate ScriptedContainerBuilder should be used depending on the script type being read. ScriptedContainerBuilder builder = new GroovyContainerBuilder(script, classLoader, parent, scope) PicoContainer parentContainer = new DefaultPicoContainer(classLoader); // create the container reference and pass them to the builder ObjectReference containerRef = new SimpleReference(); ObjectReference parentContainerRef = new SimpleReference(); parentContainerRef.set(parentContainer); builder.buildContainer(containerRef, parentContainerRef, scope, true); PicoContainer picoContainer = (PicoContainer) containerRef.get(); Interpreted script languagesNanoContainer currently supports 4 different interpreted languages for container composition. The contract for Groovy, Beanshell, Javascript and Jython scripts is defined as follows:
Groovypico = new org.picocontainer.defaults.DefaultPicoContainer(parent)
pico.registerComponentImplementation(org.nanocontainer.testmodel.DefaultWebServerConfig)
pico.registerComponentImplementation(org.nanocontainer.testmodel.WebServerImpl)
or // GroovyNodeBuilder is the default Groovy builder and it is optional to specify it // Specialised builder can be set - eg subclasses of GroovyNodeBuilder builder = new org.nanocontainer.aop.dynaop.DynaopGroovyNodeBuilder() pico = builder.container(parent:parent) { component(class:org.nanocontainer.testmodel.DefaultWebServerConfig) component(class:org.nanocontainer.testmodel.WebServerImpl) } Beanshellpico = new org.picocontainer.defaults.DefaultPicoContainer(parent);
pico.registerComponentImplementation(org.nanocontainer.testmodel.DefaultWebServerConfig);
pico.registerComponentImplementation(org.nanocontainer.testmodel.WebServerImpl);
Javascript(org.picocontainer.* and org.picocontainer.defaults.* imported by default) var pico = new DefaultPicoContainer(new ImplementationHidingComponentAdapterFactory(), parent) pico.registerComponentImplementation(Packages.org.nanocontainer.testmodel.DefaultWebServerConfig) pico.registerComponentImplementation(Packages.org.nanocontainer.testmodel.WebServerImpl) XMLThe above interpreted language scripts can equally be expressed in XML <container> <component-implementation class='org.nanocontainer.testmodel.DefaultWebServerConfig'/> <component-implementation class='org.nanocontainer.testmodel.WebServerImpl'/> </container> The DTD details the full XML syntax. |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||