The Source for Java Technology Collaboration
User: Password:



Felipe Gaucho

Felipe Gaucho's Blog

Simple example of Dependency Injection

Posted by felipegaucho on November 06, 2007 at 10:41 AM | Comments (1)

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:

ioc1.jpg

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 =
       PdfExporterFactory.getPdfExporter(
       Exporter.DEFAULT_SIGNED_PDF_EXPORTER, config);
    FootprintPublisher publisher = new JdbcPublisher(
       jdbcExporter, config, logger);
    Thread service = new Thread(publisher);
    service.start();
    while (service.isAlive()) {
       Thread.sleep(200);
    }
} catch (Exception e) {
    e.printStackTrace();
}

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 DEFAULT_SIGNED_PDF_EXPORTER = "net.java.dev.footprint.exporter.pdf.SignedPdfExporter";
		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 net.java.dev.footprint.exporter.Exporter
  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.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • The subject of a very wonderful and distinct
    I thank you for continuing excellence
    Thank you

    =========================================================================

    ليبيا
    شباب ليبيا
    libya
    منتديات
    منتديات ليبية
    غرائب وحقائق
    أحاديث شريفة
    برامج اسلامية للجوال
    مفاتيح الديجيتل
    الشيرنج
    الرسيفرات
    كتب إسلامية
    خلفيات للموبيل
    الشعر الشعبي
    الصحة والطب
    طب اسنان
    كتب طب اسنان مجانية
    برامج طبية
    تعلم الإنكليزية
    اللغة الفرنسية
    طب الإعشاب
    الخواطرالادبية
    الازياء والمكياج
    تعليم الطبخ
    الاثاث الحديث
    مقاطع كرة قدم
    المصارعه الحرة
    اهداف كوره
    الفوتوشوب
    اروع البرامج
    الدوري الليبي
    خلفيات رياضية
    المصارعة
    كورة عربية
    كرة قدم عالمية
    الدوري الإيطالي
    الدوري الاسباني
    الدوري الإنجليزي
    صور المشاهير
    انواع الحلويات
    افلام كوميدية
    احدث الافلام
    افلام
    التقنية
    تحميل افلام
    برامج
    اخر برامج الجوال
    kaspersky
    أفلام كرتون عربية
    برامج برامج كمبيوتر
    برامج حماية
    برامج اختراق
    برامج صوت
    برامج تحميل برامج احدث البرامج
    محادثة
    خلفيات الطبيعة
    برامج مبايل للتحميل
    اخبار الفن
    احدث الافلام للتحميل
    تحميل افلام رعب
    ترجمةأفلام
    الكامات
    برامج جوال
    برامج محاسبة
    برامج
    kasper
    games
    برامج
    برامج
    انترنت
    برامج صوتية
    شبكات الحاسوب
    خلفيات للويندوز
    تطويرالمواقع
    العاب
    العاب الفيديو
    games
    شفرات
    برامج مسنجر
    خلفيات شاشة
    صور ترحيبيه
    الفوتوشوب
    خلفيات طبيعة
    تطويرالمواقع
    الفوتوشوب
    مقاطع البلوتوت
    مسجات ليبية
    خلفيات
    الفلاش
    التصميم الثلاثي
    برامج الجوال
    العاب الجوال
    فيديو كليب
    مسجات
    ترددات ستالايت
    نغمات

    Posted by: libyan on May 30, 2008 at 01:52 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds