What is the difference between the Java – OSGi service tracker and the declarative service?
I'm working on OSGi services now. I have a question about using services in OSGi There are some different ways to register users Can anyone explain the difference between an OSGi service tracker and a declarative service? Which is better?
Solution
In OSGi, servicetracker is a programming method to obtain service references That is, write servicetracker code to "track" a reference to another service and let us use it when available
In contrast, declarative services (DS) allows you to declare dependencies injected into components DS is a form of dependency injection The dependency diagram between services and its start order will determine when your service starts The cardinality attribute in the DS definition allows you to declare whether the relationship is mandatory (1.. 1), with at least one (1.. n), optional (0.. 1), or multiple optional (0.. n)) When you declare a mandatory relationship, your service will not start until all dependencies are met When you declare an optional relationship, your service will start regardless of the state of the dependency, but you should note that the reference to the service in the code may be null
From a practical point of view, servicetracker is a large amount of template code written and maintained Considering the dynamic nature of OSGi services, OSGi specification allows many states to be considered DS will give you a clean way to declare and maintain your dependencies Explicit dependencies will help you keep your runtime environment consistent