Skip to main content

Simple example of Dependency Injection

Posted by felipegaucho on November 6, 2007 at 10:41 AM PST

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:

  1. An Exporter, responsible for generating the document in the file system.
  2. A Publisher, responsible to access the data source and guarantee the generation of a document for each entry.

The stable release of Footprint
provides default implementations for these interfaces:

Code sample: That's all you need to generate certificates.
Logger logger = FootprintDefaults.getDefaultFootprintLogger();

FootprintProperties config = loadConfig(...);

try {

    Exporter jdbcExporter =

       Exporter.DEFAULT_SIGNED_PDF_EXPORTER, config);

    FootprintPublisher publisher = new JdbcPublisher(

       jdbcExporter, config, logger);

    Thread service = new Thread(publisher);


    while (service.isAlive()) {



} catch (Exception e) {



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:

public interface Exporter {
String export(File outputFolder) throws Exception;
String export(File outputFolder, Map fields) throws Exception;
String export(String filename, Map 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:

  1. Create a class that realize the Exporter interface:
  2. public class MyRtfExporter implements
  3. Pass an instance of this class to the constructor of the publisher:
  4. FootprintPublisher rtfPublisher = new JdbcPublisher(new MyRtfExporter(), config, logger);

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.

Related Topics >>