Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: restore

Wiki Markup
{scrollbar}

Overriding Tapestry IoC Services

...

The strategy used to determine what object gets injected is defined inside Tapestry IoC itself; thus we can take advantage of several features of the Tapestry IoC container in order to take control over specific injections.

...

In this case, it is very easy to supply your own alternate implementation of a service, by contributing a Service Override in your module class (usually AppModule.java), like this:

Code Block
langjava
titleAppModule.java (partial)

...

  @Contribute(ServiceOverride.class)
  public static void setupApplicationServiceOverrides(MappedConfiguration<Class,Object> configuration)
  {
    configuration.addInstance(SomeServiceType.class, SomeServiceTypeOverrideImpl.class);
  }

The name of the method is not important, as long as the @Contribute annotation is present on the method.

...

Sometimes you'll want to define the override as a service of its own. This is useful if you want to inject a Logger specific to the service, or if the overriding implementation needs a service configuration:

Code Block
langjava
titleAppModule.java (partial)

...

  public static void bind(ServiceBinder binder)
  {
    binder.bind(SomeServiceType.class, SomeServiceTypeOverrideImpl.class).withId("SomeServiceTypeOverride");
  }

  @Contribute(ServiceOverride.class)
  public static void setupApplicationServiceOverrides(MappedConfiguration<Class,Object> configuration, @Local SomeServiceType override)
  {
    configuration.add(SomeServiceType.class, override);
  }

Here we're defining a service using the module's bind() method.

...

Decorating Services

Another option is to decorate the existing service. Perhaps you want to extend some of the behavior of the service but keep the rest.

Alternately, this approach is useful to override a service that is matched using marker annotations.

Code Block
langjava
titleAppModule.java (partial)

...

  public SomeServiceType decorateSomeServiceType(final SomeServiceType delegate)
  {
    return new SomeServiceType() { . . . };
  }

This decorate method is invoked because its name matches the service id of the original service, "SomeServiceType" (you have to adjust the name to match the service id).

...

Note that the object passed in as delegate may be the core service implementation, or it may be some other interceptor from some other decorator for the same service.

...

 

 

Wiki Markup
{scrollbar}