cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bart Molenkamp" <>
Subject Dependency injection in Cocoon 2.1
Date Tue, 05 Jul 2005 15:00:53 GMT

I want to get rid of the Avalon interfaces in my code. Is there
something like dependency injection (e.g. setter injection or
constructor injection - similar to Spring framework) possible using ECM?
(ECM is the component engine of Cocoon, right?)

I couldn't find anything, so I started a little "experiment" on my own.
I wrote a class ComponentHandlerImpl. This class implements all the
Avalon lifecycle interfaces. For each component, I define this class as
my default class:

<role name="com.bizzdesign.components.Hello" shorthand="hello"

<role name="com.bizzdesign.components.World" shorthand="world"

The ComponentManagerImpl class also implements Configurable, and uses
that configuration to get the name of the target class. It creates a new
instance of that class, and uses the configuration too set dependencies.
Something like:

<hello target-class="com.bizzdesign.components.HelloImpl">
  <property name="world" ref="com.bizzdesign.components.World"/>
<world target-class="com.bizzdesign.components.WorldImpl">

These are used to set dependences. The ComponentHandlerImpl will lookup
the source resolver (using the regular service manager), then invoke
setWorld() on the HelloImpl instance with the resolved World instance as
an argument (just an example).

I could easily add similair methods to replace Initializable,
Disposable, Contextualizable, ...

- cleaner code, not dependent on any Avalon interfaces. Thus reusable in
other environments, probably in the 2.2 environment too.
- allows for (unit) testing.
- user interception for components is possible (e.g. one could introduce
an AOP framework or something similair).
- Avalon is still working

- When resolving the Hello component, we get an instance of
ComponentHandlerImpl. I have to call getTarget() to get the actual

Is something (which is not much in code) worthy to add to Cocoon? Or did
I overlook some feature that already handles this?


View raw message