My Launchpad‎ > ‎

Dats Injector Library

Dats Injector Library v1.0.5 (LGPL)

Requirements : You need at least Java Runtime Environment (JRE) 5 installed.

This is my injector library, it only does lightweight dependency injection (otherwise known as inversion of control) using java 5's annotations - No XML files! :)

I wrote this before Google Guice and Seam came out, but even so, this could still be useful if you want an extremely lightweight injection framework with no library dependencies whatsoever.

Quick Instructions and Usage :

For those unfamiliar with dependancy injection - It is, very simply put, a way to inject existing 'global' objects into your objects without having to pass them by reference through messy constructors and the like.

Let's start with a practical example - We have a LanManager class that we'd like any part of our application access to, so that we can broadcast messages to our network :
	public class LanManager {
sendMessage(String message) { ... }

Ok, so somewhere in our app, probably our Main class, we obtain a LanManager from somewhere. For simplicity, let's just construct one :
	import za.dats.util.Injector;

public class Main {
public void start() {
LanManager lanManager = new LanManager();
1: Injector.register(LanManager.class, lanManager);

public static void main(String args[]) { .. start off the app .. }
Notice line 1:? That's where we connect our lanManager object with the LanManager class in the Injector framework.. pretty simple. Most people would prefer to have a LanManager interface and a deriving LanManagerImpl or something, in which case you are simply connecting the interface with a concrete object.

Enough of that, let's finish off the example. Now, somewhere in your application - let's say ChatHandler we need to use the LanManager to send a message - here's where the fun stuff are :
	import za.dats.util.Injector;
import za.dats.util.Dependant;
import za.dats.util.Inject;

public class ChatHandler {
LanManager lan;

public ChatHandler {

public void sendChat(String msg) {
And there we go. The Injector.inject(this); takes care of searching the class and any classes up the hierarchy for @Inject'able resources and puts them in for you. The @Dependant lets the injector know that it has to search this class for @Inject annotations.
Deon Moolman,
Mar 1, 2010, 8:41 PM
Deon Moolman,
Mar 1, 2010, 8:41 PM