General What is Commons Inject?

Commons Inject is an implementation of JSR 330 (Dependency Injection for Java). Or, in other words: It is a dependency injection framework, like Google Guice, the core part of the Spring Framework, Dagger, or several others.

Does Commons Inject really comply to the JSR 330 standard?

Yes, it passes the JSR 330 TCK, with and without static injection.

This means, if you have got an application, which is based on an alternative implementation of JSR 330, then it should be really easy, to port that application over to Commons Inject. In general, the only obvious thing to do would be to rewrite the configuration part. However, as Commons Injects configuration closely follows the configuration concepts from Guice, that should be really easy.

How does Commons Inject compare to other JSR 330 implementations?

The author of Commons Inject believes that the framework has the following advantages over its competitors:

  • It is very small, and lightweight. (As of this writing, it contains only 56 Java classes with 3833 lines of code, including test sources. The complete source distribution will have about only 1MB of data, without compression.
  • The emphasis is on usability, not strictness. For example, there is no need to derive subclasses of a key in order to bind generic classes. Or, you may use strings as names withput converting them to annotations.
  • With usability in mind again: It comes with meaningful extensions integrated. For example, there is a PostConstructModule, which enables initialization and resource cleanup for components. Likewise, you can have loggers, or properties injected into your beans.
  • Commons Inject is based on Java Reflection only. AFAIK, this means it would work on Android, too, unlike Guice.
  • It works standalone. The only jar file you need is commons-inject-YOURVERSION.jar. No additional stuff like Guava, AspectJ, Javassist, or whatever required.
Using Commons Inject How do I install Commons Inject?

There is no need for a special installation. Just add the jar file to your project, like you would do with any other jar file. In the case of Maven, you would simply declare a dependency like this:

<dependencies> <!-- Other dependencies ... --> <dependency> <groupId>org.apache.commons.inject</groupId> <artifactId>org.apache.commons.inject</artifactId> <version>YOURVERSION</version> <dependency/> <dependencies/>