Brian's Waste of Time

Mon, 23 Aug 2004

Two Picocontainer Idioms

Two idioms I have found very useful when working with Picocontainer:

When you refactor a component to add a dependency (new constructor arg), leave the old constructor signnature, but have it pass a null object (not null!) to the new constructor. The null object throws an exception (I like UnsupportedOperation or IllegalState personally) if anything on it is called. This solves the annoying "I don't want to add the dependency because it will break the unit test compiles" problem (side note, setter injection people like to argue that si does this for free. Really, you just get a partially initialized component, not a solution). As pico uses the greediest constructor it can find, it will use the new one.

The second idiom is using a Component and ComponentConfig where the ComponentConfig is a simple bean which holds the primitive (Strings are primitive in Java, darn it) dependencies. If you really like your XML you can use xmlbeans to generate the config classes and read them at runtime. I don't. Avoid the pull of putting all the config values in a single big config class which everything is dependent on. The phrase "everything is dependent on" should be sufficient to stop you, right there.

9 writebacks [/src/java] permanent link