I am migrating from org.apache.felix.scr
annotations to org.osgi.service.component
annotations. I have a set of Components that inherit from a common abstract class. In the felix case, I can use a @Component
annotation with the option componentAbstract=true
on the super class, and then use @Reference
annotation in the super class. I cannot find how to migrate this to osgi annotations.
Is it possible to use Component annotations in a super class of a Component? And if so, what is then the appropriate way to handle the properties and metatype generation?
So, what I am looking for, is something like this
/* No component definition should be generated for the parent, as it is
abstract and cannot be instantiated */
@Component(property="parent.property=parentValue")
public abstract class Parent {
@Reference
protected Service aService;
protected activate(Map<String,Object> props) {
System.out.println("I have my parent property: "+props.get("parent.property"));
@Override
public abstract void doSomething();
}
/* For this class, the proper Component definition should be generated, also
including the information coming from the annotations in the parent */
@Component(property="child.property=childValue")
public class Child extends Parent {
@Activate
public activate(Map<String,Object> props) {
super.activate(props);
System.out.println("I have my child property: "+props.get("child.property"));
}
public void doSomething() {
aService.doSomething();
}
}
"Components" are less formally defined than services. A service is any object that is registered in the OSGi Service Registry and can be looked up using its interface name(s). The only prerequisite is that a service should implement some interface... any interface.
The @Component annotation is the only required annotation. If this annotation is not declared for a Java class, the class is not declared as a component. This annotation is used to declare the <component> element of the component declaration.
Components are the main building blocks of your OSGi application. They have a life cycle, can register themselves as services and have zero or more dependencies. You can either use the Java API or the Java Annotations and this reference section describes both.
Class C should implement A. Then it will work.
By default BND will not process DS annotations in parent classes. You can change that with -dsannotations-options: inherit
but please see http://enroute.osgi.org/faq/ds-inheritance.html why you shouldn't!
2021-02-23 UPDATE: It seems like the page mentioned above is no longer available. I don't know if it was moved elsewhere or simply removed but its content (in Markdown format) is still available on GitHub: https://github.com/osgi/osgi.enroute.site/blob/pre-R7/_faq/ds-inheritance.md
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With