在大多数情况下,我有很多组件具有由OSGi声明服务注入的相同类.这些服务将用于执行某些逻辑,这些逻辑对于所有派生组件都是相同的.因此,为避免重复代码,最好使用抽象类.是否有可能将DI引用方法(set / unset)移动到抽象类.我正在使用Bnd.
例如:
@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 } }
我想将ServiceC的setter和executeSomethingDependendOnServiceC()方法移动到抽象类.但是在OSGi中它与Bnd注释有什么关系呢?只是使用@Component注释该类是不起作用的,因为A和D将创建抽象类的不同实例,而@Component是alsp创建实例.