...
In this case, it is very easy to supply your own alternate implementation of a service.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Contribute(ServiceOverride.class) public static void setupApplicationServiceOverrides(MappedConfiguration<Class,Object> configuration) { configuration.addInstance(SomeServiceType.class, SomeServiceTypeOverrideImpl.class); } |
...
In this example, we are using addInstance()
which will instantiate the indicated class and handle dependency resolution
Wiki Markup |
---|
{footnote}Be careful with this, because in some cases, resolving dependencies of the override class can require checking against the ServiceOverrides service, and you'll get a runtime exception about ServiceOverrides requiring itself!{footnote} |
...
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 | lang | java|||
---|---|---|---|---|
| ||||
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); } |
...
Alternately, this approach is useful to override a service that is matched using marker annotations.
Code Block | lang | java|||
---|---|---|---|---|
| ||||
public SomeServiceType decorateSomeServiceType(final SomeServiceType delegate) { return new SomeServiceType() { . . . }; } |
...
Wiki Markup |
---|
{display-footnotes} |
Wiki Markup |
---|
{scrollbar} |