@Reference or bind/unbind or get/set ; which is the right way ? | Community
Skip to main content
VeenaVikraman
New Participant
Mar 13, 2019 8:58 PM UTC

@Reference or bind/unbind or get/set ; which is the right way ?

Hi All

   This question is more or less similar to The annotation @Reference is disallowed for this location  . Today I was faced with a question. So the question was like "How will you refer a service in another service or component ? " So my obvious answer was using @Reference. But the the person told that @Reference doesn't work with new OSGI R6 annotation (which was non acceptable for me because all the projects with almost all the archetype till 17 I am able to get it work) . I never faced issue with using @Reference . So my second answer was "Ok. There was once a scenario where i got an error which said @Reference not allowed here , so then after so much of searching I found out that , it can be worked around using bind/unbind" . This answer also didn't satisfy. So then he informed me that we have to get a service using getters and setters in new OSGI R6 annotations. This was a new thing to me as I have never faced a situation where @Reference returned me null.

  After this discussion , i assumed it could be the case with a latest archetype like 17 . So I created a project with the very latest archetype and tried getting my service using @Reference annotation. It worked like as usual. So I wanted to confirm the solution mentioned by the person and after lot of searching I got few leads on how to use it . java - Resolving an OSGi service instance from a static method - Stack Overflow   . I did give it a try and it worked.

  So my question to the community is , as per OSGI R6 annotations  what is the right way to do this ? What are the implications of doing each way ? Is there any documentation which will explain on this ? When I looked for few documentations , one like feike_visser​ Using OSGi R6 annotations (>= AEM6.2) - Experience Delivers told @Reference has no change which I can also confirm from all the test I did.

Find below the sample done using Setter and getter for getting service. This worked as normal as @Reference . The problem here is I am not able to justify why it is done this way or why it works when using @Reference.

One of my colleague actually informed me that for him in latest project all the other ways gave null and so when he used setter/getter only he could get service . This has confused me more as I have never experience a null while using @Reference.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

7 replies

VeenaVikraman
New Participant
Mar 15, 2019 8:21 AM UTC

Oh okay.

Translate
arunpatidar
New Participant
Mar 14, 2019 2:07 PM UTC

Hi Veena,

Agree with Jörg.

I never faced any problem while using @Reference annotations.

But as you said, it is working with the code as you showing in screenshot.

I would suggest, you should have a look to manifest file after creating reference of service using @Reference annotation or by using getter and setter.

You would see the difference or would be same using both approach.

Translate
Arun Patidar
VeenaVikraman
New Participant
Mar 14, 2019 3:56 PM UTC

Thanks Arun and Jorg , even I didn't face issue with @Reference except for once . I was confused when someone told me that in 6.4 that doesn't work (which I never faced) at all and only getter and setter way will work and that is the right way . He said @Reference gives null. So I wanted to know in what scenario @Reference will return null for any service referred in Component/Service. Not much documentation out there though.

Translate
arunpatidar
New Participant
Mar 14, 2019 4:23 PM UTC

I know a scenario, when you mix osgi r6 and scr felix annotations. for example using osgi R6 annotations to create components/services but referencing other services using scr Reference(org.apache.felix.scr.annotations.Reference) annoations.

Translate
Arun Patidar
joerghoh
Employee
Mar 14, 2019 1:17 PM UTC

The annotation @Reference is available on both SCR and OSGI R6/R7 with pretty much the same semantics.

That's my experience and also outlined at https://blog.osoco.de/2016/05/migrating-from-the-apache-felix-scr-annotations-to-the-osgi-declarative-services-annotatio…  (which is Carsten Ziegeler's blog, and he should know it ...)

Jörg

Translate