Differences between revisions 2 and 3
Revision 2 as of 2006-05-16 14:20:32
Size: 3743
Editor: AchimHuegen
Comment:
Revision 3 as of 2006-11-02 08:30:24
Size: 3427
Editor: AchimHuegen
Comment:
Deletions are marked like this. Additions are marked like this.
Line 125: Line 125:
{{{
public class ExampleModule
{
  public TranslatorManager getTranslatorManager()
  {
    TranslatorManager manager = new TranslatorManagerImpl();
    manager = addInterceptor(manager, "hivemind.LogInterceptor", configParam1, configParam2, ....);
    return manager;
  }
}
}}}
... to be specified
Line 139: Line 129:
The methods ''service'', ''configuration'', ''autowireConstruction'', ''addInterceptor'' etc. could either be statically
imported or they could
be inherited from an ancestor.
The methods ''service'', ''configuration'', ''autowireConstruction'', ''addInterceptor'' etc. could be inherited from an ancestor.

Description

AchimHuegen, May 16 2006

Proposed Solution

A module is a plain java class that annotates its methods. Each annotated method corresponds with a service point, configuration point or contribution.

The first example demonstrates the definition of three services and their wiring.

public class ExampleModule1
{
  @Service(id = "Calculator", serviceModel="singleton")
  public Calculator getCalculator()
  {
    return new CalculatorImpl(service(Adder.class), service(Subtracter.class));
  }

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

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

The method body of a service point method is at the same time factory method, responsible for the service construction and its wiring. Note, that the service-Method offers a typed access to other services!

Autowiring Properties:

public class ExampleModule
{
  public Calculator getCalculator()
  {
    CalculatorImpl calculator = new CalculatorImpl();
    autowireProperties(calculator);
    return calculator;
  }
}

Autowiring Construction:

public class ExampleModule
{
  public Calculator getCalculator()
  {
    Calculator calculator = autowireConstruct(CalculatorImpl.class);
    return calculator;
  }
}

Define configuration points with default values:

public class ExampleModule
{
 
  @Configuration(id = "Translators")
  public List getTranslators()
  {
    List translators = new ArrayList();
    translators.add(new StringTranslator());
    translators.add(new IntegerTranslator());
    return translators;
  }
}

Contribute to a configuration point:

public class ExampleModule
{
 
  @Contribution(configuration-id = "Translators")
  public void getTranslators(List translators)
  {
    translators.add(new SmartTranslator());
  }
}

Wiring of configurations:

public class ExampleModule
{
  public TranslatorManager getTranslatorManager()
  {
    TranslatorManagerImpl manager = new TranslatorManagerImpl();
    manager.setTranslators(configuration("translators", List.class);
    return manager;
  }
}

Configurations can be any POJO:

public class ExampleModule
{
  @Configuration(id = "strutsModule")
  public ModuleConfig getStrutsModule()
  {
    return new ModuleConfigImpl();
  }

  @Contribution(configuration-id = "strutsModule")
  public void contributeToStrutsModule(ModuleConfig config)
  {
    config.addActionConfig(new ActionConfig());
    config.addFormBeanConfig(new MyFormBeanConfig());
  }
}

Add an interceptor: ... to be specified

Details

The methods service, configuration, autowireConstruction, addInterceptor etc. could be inherited from an ancestor. They make use of java 5 generics get rid of casting.

Convention over configuration

Most annotations could be replaced by conventions in the default case: For example the method name getServiceCalculator would be sufficient to define a new service point with the name calculator. contributeToStrutsModule could indicate a contribution to the configuration strutsModule which is defined by getConfigurationStrutsModule.

AnnotationSupport (last edited 2009-09-20 22:01:28 by localhost)