Dependency Injection with the Konik library

20 März 2014

Starting with version 0.2.0, the Konik library and iText-Carriage are supporting JSR 330 dependency injection.

Injectable Konik Classes

The Singleton classes InvoiceTransformer in the Konik library ITextPdfInvoiceExtractor and ITextPdfInvoiceAppender in the iText-Carriage module can now be injected into your application. The only requirement is that your Dependency Injection Framework supports JSR-330. Even though DI frameworks are widely used, the Konik library and its modules do not force you to use a DI framework at all. You can still instantiate the above classes with the new Operator.

Which Dependency Injection Frameworks are supported?

All common Dependency Injection Frameworks and JEE6 compatible Application Servers support JSR-330 Dependency Injection. This means that if your application is based on Spring, Guice, JEE6, Apache OpenWebBeans, or JBoss Weld, then you are able to inject the Konik classes mentioned above into your application. The only requirement is that the JSR-330 API javax.inject.* is on your classpath, which is not always true, as the following examples explain.

Spring and Konik example

Make sure that you use Spring above Version 3, which supports JSR-330. The dependency on the JSR-330 API are declared as optional in Spring, which means that you need to add the JSR-330 library to your application. In your project pom.xml file, add the following dependency.

<dependency>
   <groupId>javax.inject</groupId>
   <artifactId>javax.inject</artifactId>
   <version>1</version>
</dependency>

The next step is to configure Spring to search additional dependencies. This step is only necessary if you restricted Spring component scanning to certain packages beforehand. Otherwise, you are ready to go.

For Spring with an XML configuration, append the Konik library package name io.konik to the existing component scanning directive.

<context:component-scan base-package="com.myCompany, io.konik" />
@Configuration
@ComponentScan("com.myCompany, io.konik")(1)
@EnableAutoConfiguration
class Application {
// ...
1 For Java-based Spring configuration, append the io.konik package in the @ComponentScan annotation.

Other Dependency Injection Frameworks

OpenWebBeans also has JSR-330 API as an optional dependency, similar to Spring. The only minor difference is that OpenWebBeans recommends the use of a different JSR-330 dependency Jar package.

<dependency>
   <groupId>org.apache.geronimo.specs</groupId>
   <artifactId>geronimo-atinject_1.0_spec</artifactId>
   <version>1.0</version>
</dependency>

Using the Konik library with Google Guice does not need any special setup, as Google Guice is already bundled with JSR-330. This is also true for JEE6 Applications Servers. If in doubt, you should look up the reference manual of your Dependency Injection Framework, which normally goes into greater detail about this topic.

Weiterer Beiträge zum Thema Dependency Injection with the Konik library