![]() | ![]() | ![]() | ||||||||||||||||||||||
![]() | ||||||||||||||||||||||||
documentation examples changes amber (jpa) ejb database ioc jmx jsf messaging quercus remoting servlet security basic resource injection periodic task ioc appconfig | dependency injection for resources
The Dependency Injection pattern simplifies application code, and increases configuration flexibility by deferring component configuration and assembly to the container. Resin calls setters on the configured objects to assemble the resource dependencies.
Dependency injection is a term used to describe a separation between the implementation of an object and the construction of an object it depends on, and the ability for a container like Resin to resolve the dependency. 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 the dependent resources, for example. The MovieFinder example for this tutorial comes from Martin Fowler's Dependency Injection article. More details on Resin's configuration is available at the bean-style configuration page. Configuration as Assembly Line![]() The Dependency Injector pattern could also be called the Assembly pattern because it resembles an assembly line making cars.
Some important points:
Because the Assembler is independent of the code, a project could change the Assembler from Spring to Resin with no code changes. So using the Assembler/Dependency Injection pattern reduces dependencies on the framework. Only the configuration changes when changing Assemblers, not the code. While testing, the test case or the harness plays the Assembler
role, simplifying the test suite and ensuring that the code under test
is the production code. A test can create a test implementation of
the Part, e.g. In some cases, the application code can provide its own
The only code specific to the setter-based injection pattern is the addition of a setter method for the dependent resource. In many application, that setter will already be written, so no additional code would be required. Either an interface or a class can be used for the dependent resource, depending on the application's architecture. This example uses both: the MovieLister uses a dependent MovieFinder interface, and the MovieServlet uses the dependent MovieListener class. import javax.webbeans.Component; import javax.webbeans.In; @Component public class MovieListener { @In private MovieFinder _finder; ... } Configuring the MovieFinder Service <bean class="example.MovieFinderImpl"> <init> <movie director="Jackson" title="Fellowship of the Ring"/> <movie director="Jackson" title="The Two Towers"/> <movie director="Lucas" title="Star Wars"/> <movie director="Gilliam" title="Brazil"/> </init> </bean> The Dependency Injection pattern is just as useful for servlet configuration as it is for resources. This example makes the MovieLister a parameter of the servlet. The resin-web.xml will configure the servlet with the appropriate MovieLister The advantages of using dependency injection for the servlet are the same as for the resource:
Configuring the MovieServlet import javax.webbeans.In; public class MovieServlet extends HttpServlet { // Inject the MovieLister service @In private MovieLister _movieLister; ... }
|