which DI framework do you prefer: Guice or Spring

Hi,

If you needed to use DI for an existing application, which framework would you prefer: Guice or Spring?

Elad

Comments

Most likely Spring...

While Guice looks more like DI per-se, Spring it is much more than DI (though DI is at the heart of it). Some good key points is at http://www.jroller.com/habuma/entry/guice_vs_spring_javaconfig_a :
" * String identifiers - One complaint against Spring's XML configuration is the use of String identifiers and the lack of refactorability therein. It's true, that Spring's XML configuration is heavy with references to classes, properties, and other beans that are merely String values and thus difficult to refactor without a proper tool (such as the SpringIDE for Eclipse). Fortunately, both Guice and Spring JavaConfig register a win in this category, as neither relies on String identifiers in their configuration and both are easily refactorable through modern IDEs.
* Speed/Performance - Guice bills itself as faster than other containers when performing dependency injection. While I didn't run any formal benchmarks to compare Spring JavaConfig and Guice, some informal tests proved Guice's claims to be true. This is not terribly surprising, however, as Spring defines a rich set of lifecycle events around the creation of beans in the container, while I could not find any comparable lifecycle built into Guice. Without the overhead of the lifecycle events, Guice does, in fact, wire up the beans much faster than Spring.
* JAR file footprint - The Guice example above only requires two JAR files: guice-1.0.jar and aopalliance.jar. Together, these two JAR files weigh in at just over half a megabyte. Meanwhile, the Spring example requires several JAR files, including spring-javaconfig.jar, spring-beans.jar, spring-aop.jar, spring-core.jar, spring-context.jar, commons-logging.jar, aopalliance.jar, asm-2.2.2jar, cglib-nodep-2.1_3.jar, and aspectjweaver.jar, together weighing in at several megabytes in size. So, Guice is more lightweight than Spring in terms of JAR file footprint.
* Non-intrusive - Both Spring JavaConfig and Guice rely on proprietary annotations to help them do their injections. Where they differ, however, is that Guice requires placement of @Inject annotations withing the injected application classes, while Spring JavaConfig confines its use of annotations to the configuration-specific classes. Consequently, Guice-injected classes cannot be reused and compiled in a separate application without the Guice JAR file in place. Meanwhile, Spring JavaConfig leaves the application classes unaltered, enabling their unhindered reuse. Thus, in this category, Spring JavaConfig is the clear winner. (Granted, Spring does define a handful of annotations to be placed in application classes--@Required and @Transactional come to mind. But their use is not mandated to configure Spring.)
* Third-party beans - Related to the last category, I could find no direct way to inject values into a third party class using Guice. Guice-style injection requires the use of the @Inject annotation, which is useless in classes for which I do not have the source code (or classes that I do not wish to annotate with @Inject). Guice's documentation indicates that third-party components will require the creation of a custom provider, but in my opinion, this is a hack that adds complexity to something that would be very simple if the @Inject annotation were not required. Spring JavaConfig, however, keeps the configuration separate from the application classes, thus wiring a third-party class in Spring JavaConfig is no different than wiring a custom class. Spring wins again.
* Constant/literal injection - Injecting a String literal (or any other type of literal value) in Spring JavaConfig is rather straightforward. In contrast, literal injection in Guice is cumbersome, requiring the creation and use of a custom annotation to identify where the literal should be injected. Spring JavaConfig wins this category.
* AOP - The Guice documentation acknowledges that Guice's flavor of AOP is less powerful than Spring AOP (both of which are much less powerful than AspectJ). But, the Guice documentation claims that this loss of AOP power is in trade for simplicity. Honestly, I found Guice's approach to AOP (binding interceptors) non-intuitive as compared to Spring JavaConfig. The Spring JavaConfig approach required only a simple annotation with an AspectJ-style pointcut expression, whereas Guice required a confusing binding call involving "matchers" to pick out the target of the interceptor. Maybe I'm too well-versed in Spring AOP to appreciate the simplicity of Guice's interceptor-binding...but for now I must give Spring the win in this category.

Final score: Guice - 3, Spring JavaConfig - 5."

1. Configuration - Although JavaConfig is nice in terms of type/dependency safety, it's not necessarily the best choice! a) Because IDEs like Eclipse and IntelliJ (not sure about NetBeans) will notify you for any misconfiguration. b) When using XML you make your application more configurable. I have this done in my project. Administrators can change the application responsiveness and behaviour by editing an XML and restarting the server. No compilation needed.
2. JAR files footprint - I truly believe this is a non issue.
3. Intrusiveness - I disagree. If you follow the recommended design patterns and separate interfaces from implementations you have no problem. The reason is that portability between containers is a myth. It doesn't happen in reality. Once you choose your technology stack (assuming you did a good job) you're not going to change it. So if you decided that Spring is your flavour of IoC, your implementation should (!!) use Spring proprietary functionality (templates, awareness, etc.). If it doesn't then you miss the whole point and probably write tons of unnecessary code. So for me - intrusiveness is overruled.
4. Third-Party Beans - Totally agree! Providers are a major headache. It's a perfect example for where Spring saves you from writing code while Guice imposes these "factory" classes.
5. AOP - Spring's support for AspectJ is unbeatable. It's one of my favourites!!
6. Testing (wasn't mentioned) - Both unit and integration are killer features of Spring. Spring 2.5 introduces TestContext which is great!

Guice, on the other hand, is perfect for small or mostly self-contained projects.