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?
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.
I wanted a similar thing, and decided on guice:
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 5 and above, brought to you by Google. https://code.google.com/p/google-guice/
It has few dependencies, doesn't need another container/framework to operate, and is well documented.
Its perfectly normal to use DI outside of a container, and well worth the up front cost. I've saved many hours of development time because now I can easily and safely wire up dependencies by following a simple pattern, and creates consistency across the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With