Las sirenas poseen un arma más poderosa que su canto: su silencio
Franz Kafka

How-to Migrate from Spring Framework to Guice

Iteration 0 – Stable Spring Application

We start from a Spring application that is stable, all automated tests are running and has been tested by ourselves. Assume we are using Spring Test Framework.

Let’s learn to walk before learning to run. Assume that we are the only developers on the project, and that it’s a little application.

If you don’t have any Java application for following this how-to tutorial, why don’t you implement an easy code kata? For example this one : Prime Factors (you can see UncleBob’s implementation following the link).

Someday I’ll give you the code of this how-to tutorial using a slyly modified version of this kata (warning, I’m a JEE-heavy-Spring style lover).

Iteration 1 – from Spring Test Framework to JUnit Framework

Our first step is changing our Testing Framework, unplugging the Spring Test Framework and replacing it for JUnit.

Remove Spring Test dependency from pom.xml (or remove the library from your class path). Test classes must extend junit.framework.TestCase. Test methods names must begin with “test” prefix (“primeNumberTest” is not a valid test method name, use “testPrimeNumber” instead).

Using JUnit for running test implies Spring no longer injects automatically required beans. We can choose to either use manually Spring API for create our Context and getting beans from it or use no ID at all. Let’s chose the second option to illustrate how an application works with 3 different injection’s mechanism (java old style, Guice style and Spring style).

All testing running? All green? Let´s continue.

Iteration 2: Dependency Injection: from Spring Framework to Guice Framework

Create Java classes for your Binding Modules (no need to say, into a new package for them). Remove your now useless XML Spring configuration files.

@Override

protected void configure() {

bind(RomanNumberBO.class).to(RomanNumberBOImpl.class);

bind(RomanNumbersValidation.class).to(RomanNumbersValidationImpl.class);

}

For each property, use Guice injector initialization them. You may now consider removing Setters of this properties, those which only purpose were being invocated by Spring in order to inject the dependency.

private RomanNumberBO romanNumberBO = Guice.createInjector(new RomanNumberModule()).getInstance(RomanNumberBO.class);

Test again. All right? All green? Then we’re done.

Migrating larger applications shared with more developers

Looks pretty easy, don’t you think? Migrating small applications where we are the only developers are easy. Big shared applications are not so easy to refactor, so don’t underestimated the task.

First step should be communicating the plan to all the members of your team, and close the details of the plan with them.

Second, all members who will participate in the migration process should complete a small migration process like the described above, or participate in “pair” programming session where one “expert” migrate one little module while the other are watching. If you prefer, a good document explain it could work too. My recommendation, both of them: first write the document, share it, be sure it’s read, and “pair” programming session.

The best strategy probably is starting from a 100% vs 0% modules used by old technology vs new technology, and as both technologies live together, begin migrate one by one modules from the littler and less critical until we reach the desired 0% vs 100%.

Continue with both technologies for a while (a few production deliveries) and when you feel comfortable, unplug all the remains of the old technology.

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…?

Google Guice Dependency Injection Framework

Google GuiceGoogle Guice is an open source lightweight Dependency Injection Framework (D.I.).

Five years ago Google needed a Java D.I. Framework. Spring Framework was practically the only option but did not suited exactly to their needs, so they decided to develop their one D.I. Framework inspired on Spring. Since 2006 Google is running Guice in critical applications.

Google released the project so now is an open source project. Java 1.5 or later required if you want to use Generics or Annotations.

It’s light, it’s easy, it’s quick… why don’t you take a taste of it?

Comenta