Dios no juega a los dados
Albert Einstein

Java Dependency Injection: Guice or Spring?

Depending on your needs. Comparing both of them is like comparing a MP3 mobile player to an MP3 radio car. Spring provides you more and more powerful features and not only Dependency Injection (D.I.) : the MP3 radio car let you drive as you listen to your favorite music, but it’s harder to use if you want to spinning.

Google GuiceAlso, as you can use an MP3 mobile player in a car while a friend is driving, you can use Guice as D.I. Framework while Spring Framework is dealing with another stuffs.

Features

Guice is just a D.I. Framework, nothing more (but nothing less), so it’s quite light.

Spring gives you D.I. as part of its more featured (and heavy) core, but many other features are available that you can easily plug in (MVC, Testing, Security, Web Services Integration…). Also it’s easy to integrate with other Frameworks like Struts2 or Hibernate.

Configuration

Guice use no XML configuration files, but Java Modules classes instead where you define which bean implementation Guice will return when each Module is used. Also Annotations are supported.

Spring use XML configuration files for the same purpose using Java Beans name as identifiers, keeping Java language only for implementing your application features. Recently I’ve discovered @Autowired Annotation’s true power, keeping your XML configuration files much lighters. Also Annotations are supported.

Bean Injection

Using Guice, you must initialize using Guice injectors or Guice Annotations instead using constructors (“new”).

Spring not only automate beans dependencies injection, but also automate your beans creation process, so you will not need to use constructors: just implement all your attributes with private scope and generate public getters and setters methods and Spring will do all the work for you.  This solution is more elegant and less coupled. You can also use Annotations.

Testing

The only difference is that Spring Framework provides you  their own optional Testing Framework based on JUnit.

And finally, your champion is…?

Dejanos tu Comentario

Nombre: (Requerido)

E-Mail: (Requerido)

Sitio WEB:

Comentario:

Comenta