In most cases I have a lot of components which are having the same classes to be injected by an OSGi Declarative Service. The services will be used to execute some logic which is the same for all derived components. Therefore to avoid duplicated code it would be the best to use abstract classes. Is there any possibility to move the DI reference methods (set/unset) to an abstract class. I'm using Bnd.
For Example:
@Component
public class B implements IA {
private ServiceC sc;
@Reference
public void setServiceC(ServiceC sc) {
this.sc = sc;
}
public void execute() {
String result = executeSomethingDependendOnServiceC();
// do something with result
}
protected String executeSomethingDependendOnServiceC() {
// execute some logic
}
}
@Component
public class D implements IA {
private ServiceC sc;
@Reference
public void setServiceC(ServiceC sc) {
this.sc = sc;
}
public void execute() {
String result = executeSomethingDependendOnServiceC();
// do something different with result
}
protected String executeSomethingDependendOnServiceC() {
// execute some logic
}
}
I want to move the setter for ServiceC
and the method executeSomethingDependendOnServiceC()
to an abstract class. But how does it look like in OSGi in connection with Bnd annotation. Just annotate the class with @Component is not working, because A
and D
will create different instances of the abstract class and the @Component is alsp creating an instance.
Maybe someone experience the same problem and give me some advices how a workaround could look like. At least a best practice solution would be fine as well :)
componentAbstract
for theComponent
annotation. See: felix.apache.org/site/… -> Abstract Service Descriptions. But there are no other information about this, even in the OSGi Sepcification. Do you know something about it? – Mcmillin