Simple example of Dependency Injection
Footprint Project is gaining momentum since its stable release, with a growing number of JUGs and other
groups using the library to produce signed certificates for events and courses.
Some of these early adopters are asking me how to modify the way Footprint publish the certificates,
and also if there is a set of configuration they can use in order to avoid checkout the project from
the SVN repository and do it programmatically.
Well, you actually cannot escape from programming, but instead of digging details
of the Footprint library, you can use Dependency Injection to minimize your effort.
How Footprint publishes the signed PDF files?
First of all, a brief explanation on how Footprint generates documents - shown in the diagram below:

|
The documents are generated by two classes:
The stable release of Footprint
|
|
Using Constructor Injection to modify the behavior of the Exporter
The core interface of Footprint is the Exporter, which describe the following set of methods:
package net.java.dev.footprint.exporter;
public interface Exporter {
String <font color='darkblue'>DEFAULT_SIGNED_PDF_EXPORTER</font> = "net.java.dev.footprint.exporter.pdf.SignedPdfExporter";
String export(File outputFolder) throws Exception;
String export(File outputFolder, Map<string, string=""> fields) throws Exception;
String export(String filename, Map<string, string=""> fields) throws Exception;
}
Following the Dependency Injection pattern proposed by Martin Fowler, the Footprint
uses Construction Injection to allow different implementations of the Exporter interface. In order to produce different documents - other than signed PDF files - or to change the way the documents are generated, you must do:
- Create a class that realize the Exporter interface:
- Pass an instance of this class to the constructor of the publisher:
public class <strong>MyRtfExporter</strong> implements net.java.dev.footprint.exporter.Exporter
FootprintPublisher rtfPublisher = new JdbcPublisher(new <strong>MyRtfExporter</strong>(), <em>config</em>, <em>logger</em>);

That's it. Dependency Injection is an overhyped term used to promote the idea of an advanced usage of Design Patterns, but simply posting: dependency injection is a realization of an interface passed as parameter of a constructor or a method.. The so called inversion of control means you are creating a program based on abstract concepts - the interfaces - but the concrete way your program will produce its results will come from outside the code: it will be injected. Your code will not control the way the interfaces will be realized, the user of your code will do that ;)
Final tips:
- Footprint uses reflection to load the instances of the injected implementations.
- Don't forgett to include your custom implementations of Exporter in the same classpath of the footprint-core.jar.
- Footprint Project is licensed under LGPL, and the full source code is available at the SVN repository.
- Printer-friendly version
- felipegaucho's blog
- 2461 reads





