Status

Annocon is no longer actively maintained. The concepts and ideas have been transfered to the Apache HiveMind project. HiveMind 2 features annotated modules that share the same vision that motivated Annocon.

Introduction

Annocon is an inversion of control container (IoC) based on Java 5 annotations.

For a general introduction to the IoC principle it is recommended to read these articles: Design Better Software with the Inversion of Control Pattern and IoC Pattern. Additionally the documentation of the libraries which mainly inspired annocon could be helpful: HiveMind, Spring Framework.

Unlike other containers annocon doesn't use xml descriptors to define which beans/services/components exist and how they collaborate. Instead annotations in addition to plain java are used for defining and constructing services and configurations.

Annocon is realized using an Aspect Oriented Programming (AOP) approach. An annocon context is aspectized using the dynaop framework. Aspects are used to mixin code for service creation, configuration building and lifecycle into the context class.

To get a quick impression how annocon works, read on to the next example. A more thorough introduction is to be found in the manual.

Example

This is the most basic example that shows how to define an annocon context. The context is responsible for defining and linking services. This is comparable to the HiveMind registry or the Spring IoC.

@Context 
public class BasicContext
{
  @Service(id = "Calculator", instantiationModel="prototype")
  public Calculator getCalculator()
  {
    return new Calculator(getAdder(), getSubtracter());
  }

  @Service(id = "Subtracter", instantiationModel="singleton")
  private Subtracter getSubtracter()
  {
    return new Subtracter();
  }

  @Service(id = "Adder", instantiationModel="singleton")
  private Adder getAdder()
  {
    return new Adder();
  }

}

The example demonstrates how to define three services Calculator, Subtracter and Adder. The Calculator service has a dependency on both other services which are injected via the constructor. The services use different instantiation models. The Calculator uses the prototype model, which creates a new calculator instance on each call of the getter method. The other services both use the singleton model. In this case annocon ensures that each call to the getter method returns the same service instance. The next code snippet shows how to setup the context and query a service:

ContextFactory factory = new ContextFactory();
BasicContext context = (BasicContext) factory.buildContext(BasicContext.class);

Calculator calculator = context.getCalculator();
double sum = calculator.add(10, 20);
     

This service query demonstrates an important strength of annocon: No type casts. Services can be aquired stongly typed without the need to hand in ids or interfaces.

Additionally annocon offers autowiring to make service wiring even easier.

The full source code can be found here.

Now, continue with the manual.