Providing DI Labels in Abstract Classes

In most cases, I have many components that have the same classes that must be introduced by the OSGi declaring service. Services will be used to execute some logic that will be the same for all derived components. Therefore, to avoid duplication of code, it would be better to use abstract classes. Is it possible to move DI reference methods (set / unset) to an abstract class. I am using Bnd.

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 executeSomethingDependendOnServiceC() method to an abstract class. But what does it look like in OSGi in connection with the Bnd annotation. Just comment on the class with @Component does not work, because A and D will create different instances of the abstract class, and @Component is alsp creating the instance.

Maybe someone is experiencing the same problem and giving me some tips on what a workaround might look like. At least the best solution would be also good :)

+7
source share
1 answer

DS annotations must be included in the class instance for the component. Superclass annotations are not supported. There is a proposal for a change in a future version of the specification.

What you can do is move the method to the superclass, but you will have to trivially redefine the method in the subclass so that you can annotate it in the subclass.

+4
source

All Articles