⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.xtp

📁 RESIN 3.2 最新源码
💻 XTP
字号:
<document>  <header>    <product>resin</product>    <title>Dependency Injection for Resources</title>    <type>tutorial</type>    <description><p>The Dependency Injection pattern simplifies applicationcode, and increases configuration flexibility by deferringcomponent configuration and assembly to the container.Resin calls setters on the configured objects to assemble theresource dependencies.</p></description> <tutorial-startpage>movies?director=Jackson</tutorial-startpage></header>  <body>    <localtoc/><s1 title="Files in this tutorial"><deftable><tr>  <td><viewfile-link file="WEB-INF/resin-web.xml"/></td>  <td>Configures the movie application</td></tr><tr>  <td><viewfile-link file="WEB-INF/classes/example/Movie.java"/></td>  <td>The movie bean.</td></tr><tr>  <td><viewfile-link file="WEB-INF/classes/example/MovieFinder.java"/></td>  <td>The MovieFinder interface</td></tr><tr>  <td><viewfile-link file="WEB-INF/classes/example/MovieFinderImpl.java"/></td>  <td>A MovieFinder implementation</td></tr><tr>  <td><viewfile-link file="WEB-INF/classes/example/MovieLister.java"/></td>  <td>The MovieLister to be configured with the finder implementation</td></tr><tr>  <td><viewfile-link file="WEB-INF/classes/example/MovieServlet.java"/></td>  <td>The MovieServlet to be configured with the finder implementation</td></tr><tr>  <td><viewfile-link file="WEB-INF/classes/META-INF/web-beans.xml"/></td>  <td>web-beans.xml marks the directory as containing components.</td></tr></deftable></s1><s1 title="Dependency Injection"><p><i>Dependency injection</i> is a term used to describe a separation between theimplementation of an object and the construction of an object itdepends on, and the ability for a container like Resinto resolve the dependency.</p><p>Since the container instantiates and assembles the dependencies,the code is simpler and the configuration is more flexible.It's easy to substitute test implementations as thedependent resources, for example.</p><p>The MovieFinder example for this tutorial comes fromMartin Fowler's<a href="http://www.martinfowler.com/articles/injection.html">DependencyInjection</a> article.</p><p>More details on Resin's configuration is available at the <a href="doc|ioc-bean.xtp">bean-style configuration</a> page.</p><s2 title="Configuration as Assembly Line"><figure src="assembler-eg1.gif"/><p>The Dependency Injector pattern could also be calledthe Assembly pattern because it resembles an assemblyline making cars.</p><p></p><ul><li>Parts are interchangable components like wheels.  The parts mightalso be assembled like an engine in a car.</li><li>Parts are attached to the Chassis like a car's frame receiving anengine.</li><li>The Registry is holds partially-completed parts like a factoryconveyor belt.</li><li>The Assembler provides the Registry and assembles the Chassis andParts into a completed resource.</li></ul><p>Some important points:</p><ul><li>The application code (Chassis and Parts) is independent of theAssembler.</li><li>Parts are interchangeable.</li><li>The code needs to select an assembly pattern, e.g. Setter Injection.</li></ul><p>Because the Assembler is independent of the code, a project couldchange the Assembler from Spring to Resin with no code changes.So using the Assembler/Dependency Injection pattern reducesdependencies on the framework.  Only the configuration changeswhen changing Assemblers, not the code.</p><p>While testing, the test case or the harness plays the Assemblerrole, simplifying the test suite and ensuring that the code under testis the production code.  A test can create a test implementation ofthe Part, e.g. <code>TestMovieFinder</code>, for testing.</p><p>In some cases, the application code can provide its own<code>assemble()</code> method for situations where the container isincapabile of assembling the components.  For example, the<code>MovieServlet</code> could create an <code>assemble()</code>method that grabbed the <code>MovieLocator</code> from JNDI.</p></s2></s1><s1 title="Code for the Dependency Injection pattern"><p>The only code specific to the setter-based injection patternis the addition of a setter method for the dependent resource.In many application, that setter will already be written, sono additional code would be required.</p><p>Either an interface or a class can be usedfor the dependent resource, depending on the application'sarchitecture.  This example uses both: the MovieLister uses a dependentMovieFinder interface, and the MovieServlet uses the dependentMovieListener class.</p><example>import javax.webbeans.Component;import javax.webbeans.In;@Componentpublic class MovieListener {  @In private MovieFinder _finder;  ...}</example></s1><s1 title="Configuration"><example title="Configuring the MovieFinder Service">&lt;bean class="example.MovieFinderImpl"&gt;  &lt;init&gt;    &lt;movie director="Jackson" title="Fellowship of the Ring"/&gt;    &lt;movie director="Jackson" title="The Two Towers"/&gt;    &lt;movie director="Lucas" title="Star Wars"/&gt;    &lt;movie director="Gilliam" title="Brazil"/&gt;  &lt;/init&gt;&lt;/bean&gt;</example></s1><s1 title="Dependency Injection for Servlets"><p>The Dependency Injection pattern is just as useful for servletconfiguration as it is for resources.  This example makes theMovieLister a parameter of the servlet.  The resin-web.xml will configurethe servlet with the appropriate MovieLister</p><p>The advantages of using dependency injection for the servlet arethe same as for the resource:</p><ul><li>The servlet code becomes simpler.</li><li>The servlet is no longer dependent on JNDI.</li><li>The servlet is more easily testable by configuring it with testversions of the MovieListener.</li></ul><example title="Configuring the MovieServlet">import javax.webbeans.In;public class MovieServlet extends HttpServlet {  // Inject the MovieLister service  @In private MovieLister _movieLister;  ...}</example></s1><s1 title="See also"><ul><li><a href="doc|resin-ioc.xtp">Resin/WebBeans Dependency Injection</a></li><li><a href="../db-jdbc-ioc/index.xtp">Servlet DataSource Configuration</a></li><li><a href="../hessian-ioc/index.xtp">Hessian with Dependency Injection</a></li></ul></s1>  </body></document>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -