4

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.

Jens
  • 20,533
  • 11
  • 60
  • 86
Vishal--JAVA--CQ
  • 188
  • 1
  • 2
  • 18
  • Have a look at the following SO questions, maybe they answers your question already: https://stackoverflow.com/questions/28104737/ or https://stackoverflow.com/questions/15571394/ – Jens Apr 02 '19 at 05:29
  • I've already read those posts but unfortunately no other post on stack overflow talks about service annotation in detail. – Vishal--JAVA--CQ Apr 03 '19 at 02:08

1 Answers1

5

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

Marcos Zolnowski
  • 2,751
  • 1
  • 24
  • 29
  • So essentially, the @service annotation was only to declare a component as a object which can be injected into other objects. And now with R6 annotations, this is declared as a attribute to the component annotation? Is this understanding correct? btw Love the Bonus article. – Vishal--JAVA--CQ Apr 03 '19 at 02:07
  • 1
    Yes, but `@Service` is a companion annotation, I doubt it will work without `@Component`. You're welcome :) – Marcos Zolnowski Apr 03 '19 at 20:38
  • 1
    It wont. But with R6 you dont have to include service annotation at all. Thankfully OSGi annotations implement it via a attribute in the component annotation. – Vishal--JAVA--CQ Apr 04 '19 at 00:39
  • 1
    To clarify slightly more... *some* components need to be services, so they can be injected into other components. This is because they implement some functionality that is described by a Java interface. However some components do *not* need to be services because they provide "top-level" functionality that interacts directly with the user or the environment. For example, a component that implements a web server. Or one that implements a GUI shell. Or some kind of batch component that only interacts with files. The point is that not all components are just sitting around waiting to be called. – Neil Bartlett Apr 05 '19 at 07:27