Search code examples
serviceannotationsosgiaemsling

What is the @Service annotation in Apache Felix?


I've read several articles about the difference between @Service and @Component. Now I understand that @Component is user to annotate an object as an OSGi component and its lifecycle will then be managed by OSGi. However, what is the need to declare an object as service with @Service is unknown. What happens if you write your business logic in a object that is declared as a component?

I also want to know what does the below statement means:

Components can refer/call (using container injection – @Reference) other services but not components. In other words, a component cannot be injected into another component / service. Only services can be injected into another component.


Solution

  • Avoid using annotations from Felix SCR (package org.apache.felix.scr.annotations).

    http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html :

    The annotations itself do not support the new features from R6 or above. It is suggested to use the official OSGi annotations for Declarative Services instead.

    Use @Component from package org.osgi.service.component.annotations, this annotation replaces both @Component and @Service from Felix.

    • Question 2

      What happens if you write your business logic in a object that is declared as a component?

      Happens to work fine.

    • Question 3

      I also want to know what does the below statement means:

      Components can refer/call (using container injection – @Reference) other services but not components. In other words, a component cannot be injected into another component / service. Only services can be injected into another component there.

      This is how components share functionality in OSGi. They offer their features as OSGi services. But when OSGi injects the object into the reference, you get your component.

    BONUS: Read this article: https://medium.com/adobetech/using-the-osgi-declarative-service-in-aem-6-4-21102f649d54