Search code examples
javadependency-injectionframeworksinversion-of-control

Dependency Injection in plain Java


I have a large application written in Java SE (JdK 1.7). But it is getting harder to maintain. I thought it would be a nice idea to introduce DI into this project. As long as I have experience with Java EE framework, i would like to know, what is the most common way to implement the dependency injection when one does not use any container like Glassfish or Spring framework. I read about Weld and Guice, but have no idea which one is better and is it even "normal" to use such a technique as DI outside a container all on its own.

How can I benefit from IoC in Java SE7 and is it even reasonable? If it is, which is the library to use for DI in such a plain project?


Solution

  • I've used both Guice (a lot) & Spring (a little).

    Spring is big but modular. This means that if you only want a little you can set it up to only use a little and as all the other modules follow the same patterns used in the core you can pick 'em up easily. However the tutorials etc can meander into Spring at large which can confuse matters.

    Guice is small and targeted. It only contains a little and so is easy to learn. But offers little help when you want that bit more.

    IMO Guice is good if you want to get going quickly and don't have anyone with Spring XP on board. While it has less tutorials it is also a lot simpler to get your head around and the tutorials are generally more focused.

    Spring is good if you already has someone with Spring XP on board or you intend on using more Spring-y goodness in the short term.