T
- the aspect servicepublic interface ServiceAspectBuilder<T> extends ComponentBuilder<ServiceAspectBuilder<T>>, ServiceCallbacksBuilder<T,ServiceAspectBuilder<T>>
The aspect will be applied to any service that matches the specified interface and filter (if any). For each matching service an aspect will be created based on the aspect implementation class. The aspect will be registered with the same interface and properties as the original service, plus any extra properties you supply here. Multiple Aspects of the same service are chained and ordered using aspect ranks.
Code example which provides a "LogService" aspect that performs spell-checking of each log message. The aspect decorates a LogService. The aspect also depends on an Dictionary service that is internally used to perform log spell checking. The LogService and Dictionary services are injected in the aspect implementation using reflection on class fields:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
aspect(LogService.class, asp -> asp.impl(SpellCheckLogAspect.class).rank(10).withSvc(Dictionary.class));
}
}
Same example, but using callbacks for injecting LogService and Dictionary services in the aspect implementation class:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
aspect(LogService.class, asp -> asp
.impl(SpellCheckLogAspect.class).rank(10)
.add(SpellCheckLogAspect::setLogService)
.withSvc(Dictionary.class, svc -> svc.add(SpellCheckLogAspect::setDictionary)));
}
}
Modifier and Type | Method and Description |
---|---|
ServiceAspectBuilder<T> |
autoConfig()
Injects the aspect in all fields matching the aspect type.
|
ServiceAspectBuilder<T> |
autoConfig(boolean autoConfig)
Configures whether or not the aspect service can be injected in all fields matching the aspect type.
|
ServiceAspectBuilder<T> |
autoConfig(java.lang.String field)
Injects the aspect service on the field with the given name.
|
ServiceAspectBuilder<T> |
filter(java.lang.String filter)
Specifies the aspect service filter.
|
ServiceAspectBuilder<T> |
rank(int ranking)
Specifies the aspect ranking.
|
autoAdd, autoConfig, autoConfig, build, composition, composition, composition, debug, destroy, destroy, destroy, destroy, factory, factory, factory, factory, factory, impl, init, init, init, init, properties, properties, properties, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, provides, start, start, start, start, stop, stop, stop, stop, withBundle, withCnf, withCnf, withCnf, withFuture, withSvc, withSvc, withSvc, withSvc, withSvc, withSvc
add, add, add, add, add, add, add, add, add, add, add, add, add, callbackInstance, change, change, change, change, change, change, change, change, change, change, change, change, change, remove, remove, remove, remove, remove, remove, remove, remove, remove, remove, remove, remove, remove, swap, swap, swap, swap, swap, swap, swap, swap, swap
ServiceAspectBuilder<T> filter(java.lang.String filter)
filter
- the filter condition to use with the service interface the aspect will apply onServiceAspectBuilder<T> rank(int ranking)
ranking
- the aspect rankingServiceAspectBuilder<T> autoConfig()
ServiceAspectBuilder<T> autoConfig(boolean autoConfig)
autoConfig
- true if the aspect service can be injected in all fields matching the dependency typeServiceAspectBuilder<T> autoConfig(java.lang.String field)
field
- the field name where the aspect service must be injected