My team is researching dependency injection frameworks and is trying to decide between using Google-Guice and PicoContainer.
We are looking for several things in our framework:
- A small code footprint – What I mean by a small code footprint is we don’t want to have dependency injection code litter everywhere in our code base. If we need to refactor down the road, we want it to be as easy as possible.
- Performance – How much overhead does each framework have when creating and injecting objects?
- Ease of use – Is there a large learning curve? Do we have to write mounds of code to get something simple working? We want to have as little configuration as possible.
- Community size – Larger communities usually means that a project will continue to be maintained. We don’t want to use a framework and have to fix our own bugs 😉 Also any questions we have along the way can (hopefully) be answered by the framework’s developer/user community .
Comparisons of the two frameworks against the listed criteria would be greatly appreciated. Any personal experiences that help to compare the two would also be extremely helpful.
Disclaimer: I’m fairly new to dependency injection so excuse my noob-ness if I asked a question that isn’t pertinent to this discussion.
Advertisement
Answer
You may want to include Spring in your list of Dependency Injection frameworks you are considering. Here are some answers to your questions:
Coupling to the framework
Pico – Pico tends to discourage setter injection but other than that, your classes don’t need to know about Pico. It’s only the wiring that needs to know (true for all DI frameworks).
Guice – Guice now supports the standard JSR 330 annotations, so you do not need Guice specific annotations in your code anymore. Spring also supports these standard annotations. The argument that the Guice guys use is that without a Guice annotation processor running, these shouldn’t have an impact if you decide to use a different framework.
Spring – Spring aims to allow you to avoid any mention of the Spring framework in your code. Because they do have a lot of other helpers / utilities etc. the temptation is pretty strong to depend on Spring code, though.
Performance
Pico – I’m not too familiar with the speed characteristics of Pico
Guice – Guice was designed to be fast and the comparison mentioned in the reference has some numbers. Certainly if speed is a primary consideration either using Guice or wiring by hand should be considered
Spring – Spring can be slow. There has been work to make it faster and using the JavaConfig library should speed things up.
Ease of use
Pico – Simple to configure. Pico can make some autowire decisions for you. Not clear how it scales to very large projects.
Guice – Simple to configure, you just add annotations and inherit from AbstractModule to bind things together. Scales well to large projects as configuration is kept to a minimum.
Spring – Relatively easy to configure but most examples use Spring XML as the method for configuration. Spring XML files can become very large and complex over time and take time to load. Consider using a mix of Spring and hand cranked Dependency Injection to overcome this.
Community Size
Pico – Small
Guice – Medium
Spring – Large
Experience
Pico – I haven’t had much experience with Pico but it is not a widely used framework so it will be harder finding resources.
Guice – Guice is a popular framework and its focus on speed is welcome when you’ve got a large project that you’re restarting a lot in development. I have a concern about the distributed nature of the configuration i.e. it’s not easy to see how our whole application is put together. It’s a bit like AOP in this respect.
Spring – Spring is usually my default choice. That said, the XML can become cumbersome and the resulting slowdown annoying. I often end up using a combination of hand crafted Dependency Injection and Spring. When you actually need XML based configuration, Spring XML is quite good. Spring also put a lot of effort into making other frameworks more Dependency Injection friendly which can be useful because they often use best practice when doing so (JMS, ORM, OXM, MVC etc.).