Configured: Rule Driven Dependency Injection

Configured is a (mini) dependency injection framework for Java. It differs from standard approaches, because rather than manually building your objects (either through bean definitions or whatever), you infer how they should be built via rules. It came into being when I found I was just too lazy to write any more bean definitions in xml or annotate another property with @Resource or @Autowired.

The general idea is of convention over configuration, but where the convention is explicitly visible to you as the developer, not hidden away inside the framework. The conventions are in the rulebases: for any given class being built or wired by Configured you (may) indicate a rulebase to be used, otherwise Configured just uses the rulebase of the object's parent in the object graph, or the rulebase you nominated as the default if there is no parent object.

Configured's default way of being... configured is via annotations (with rulebases defined as Spring beans) however one of the key points of using Configured is that most of your objects are unaware of Configured (i.e. aren't annotated at all - at least not with any Configured annotations) and also aren't being referred to as xml bean definitions. Configured allows for and fully supports specifying its behaviour at the field level, but the intention is that your rulebase specifies the way you always do things and that if you find you have classes which don't fit this pattern, you use a different rulebase to wire them, rather than adding special annotations or writing out verbose xml bean definitions.

Another key point of Configured is the idea that not every object in your object graph should need to be a bean (or equivalent thingy, depending on your IOC Container), and that it'd be really useful if we could just tell the container how to automatically build certain sorts of objects (think transformer objects, strategy objects, maybe even DAOs...) on demand and manage them by itself.

Although Configured isn't really tied to any framework in particular, it's mainly intended to be something you would use inside of Spring. To use Configured with Spring, you define some rule beans in your application context (or use some of the ones that come with the library), include the Configured bean post processor, annotate the particular top level classes in your application you want to wire via rules with the @ConfiguredByRules annotation (or wrap them in Configured's BeanGraphAutowiringServiceFactoryBean if you prefer) and away you go.

Behind the scenes, when a rule resolves a dependency (which may be by retrieving the bean from an application context, instantiating it or whatever), it tells Configured's RuleBasedBeanFactory whether or not to apply the same rules to the object it's returning, which will result in rules getting applied to each of the properites of the returned object, and so on and so on, effectively recursively creating and wiring up the whole object graph.

News

  • 24th Jan 2009 - Configured 0.1 is still under development, so not much news yet. Mainly focusing on improving documentation, writing examples and improving test coverage.