(blog-of ‘Alex’)

A personal blog about software development and other things. All opinions expressed here are my own, unless explicitly stated.

Micro Dependency Injection Library

It is not uncommon to see dependency injection frameworks in modern Java applications, in fact, it looks like there is unwritten consensus, that inversion of control is indeed the guiding principle in implementing dependency management in modern applications.

However, two major players in this area, Spring and Guice are quite big, and I decided to write (for fun) my own dependency injection framework, micro-di.

Oh, and by the way, I know that there are other DI frameworks and some of them are very lightweight and fast. But hey, I told you, I did it for fun!

Here is a quick, 30-second illustration of micro-di use:

public interface Inferior {
    int foo();
}

public interface Superior {
    int bar();
}

public class InferiorImpl implements Inferior {
    @Override public int foo() { return 1; }
}

public class SuperiorImpl implements Superior {
    @Resource protected Inferior inferior;

    @Override public int bar() { return 10 + inferior.foo(); }
}

@Test
public void shouldInjectOneBeanWithinAnother() {
    context.registerBean(new SuperiorImpl());
    context.registerBean(new InferiorImpl());

    final Superior superior = context.getBean(Superior.class);
    assertEquals(11, superior.bar());
}

See, I told you it is simple!

The library is released under Apache 2.0 license.

If you’re interested you can find a bit more details in the project’s README.md.

Have fun!