Tuesday, May 8, 2007

An Introduction to Commons IoC

Not to be outdone by the Dependency Injection Meta-Container, the Jakarta project is proud to announce Commons IoC.

In keeping with the proud tradition of Commons projects like Commons Logging, Commons IoC aims to bridge the wide array of Inversion of Control frameworks with a single API.

Accordingly, using a single programming model, users of Commons IoC can now build an injected-dependency application that can seamlessly switch, with a minimum of coding changes, between IoC from:

  • Spring Framework
  • Pico Container
  • HiveMind
  • Guice
Although this doesn't span all IoC frameworks, Commons IoC plans to continue to bolster support in later releases as necessary.

Configuration
Here's an example of injection in the Spring framework:
<bean id="exampleBean" class="examples.ExampleBean">

<i class="lineannotation"><span class="lineannotation"><!-- setter injection using the nested <tt class="literal"><ref/></tt> element --></span></i>
<property name="beanOne"><ref bean="anotherExampleBean"></property>

<i class="lineannotation"><span class="lineannotation"><!-- setter injection using the neater 'ref' attribute --></span></i>
<property name="beanTwo" ref="yetAnotherBean">
<property name="integerProperty" value="1">
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean">
<bean id="yetAnotherBean" class="examples.YetAnotherBean">


One from Hivemind:
service-point(id=CayenneService interface=markko.hivemind.CayenneService) {
invoke-factory(service-id=hivemind.BuilderFactory) {
construct (class=markko.hivemind.CayenneServiceImpl) {
initialize-method (method=init)
}
}
interceptor (service-id=hivemind.LoggingInterceptor)
}


And one from Guice:
public class MyModule extends AbstractModule {
protected void configure() {
bind(Service.class)
.to(ServiceImpl.class)
.in(Scopes.SINGLETON);
}
}

Notice how each of these is significantly different. Not only are they different in format, but they're different in intent. After some discussion, Commons IoC has elected to take another page from the Commons Logging and simply allow the users of Commons IoC to configure their injection within the base implementation, thereby retaining the full power of your chosen IoC tool's configuration.

Commons IoC in Action
So, having configured your application for injection, how do you use Commons IoC? You simply define your objects using one of these protocols, which Commons IoC supports for injecting dependencies into an object.

Constructor Injection:
public class MyClass()
{
private MyDependency depend;
public MyClass( MyDependency depend ) { this.depend = depend; }
}


Setter Injection, e.g.:
public class MyClass( )
{
private MyDependency depend;
public void setMyDependency( MyDependency depend ) { this.depend = depend; }
}


I hope this has given you a new understanding of Commons IoC and how to use it in your projects.

(yes, i'm kidding; really)

3 comments:

Anonymous said...

Are you really kidding? I was unable to find any such component on the Jakarta project site.

Geoffrey Wiseman said...

Yes, I'm kidding. (Which is why I ended the post with a notice of kidding).

Just poking a little fun at Commons Logging, mostly.

Anonymous said...

thank god you're kidding.