Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

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

Avatar

Community Advisor

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_visserUsing 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.

1711121_pastedImage_4.png

7 Replies

Avatar

Employee Advisor

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-declarativ...  (which is Carsten Ziegeler's blog, and he should know it ...)

Jörg

Avatar

Community Advisor

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.



Arun Patidar

Avatar

Community Advisor

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.

Avatar

Community Advisor

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.



Arun Patidar

Avatar

Employee Advisor

mixing both types of annotations is possible, because in the end you create Declarative Services metadata, which are stored as XML inside the bundle on build time. During runtime you cannot decide if this has been created using Felix SCR annotations or by OSGI annotations.

If there has ever been an issue, than not with this combination, but by some other cause.

Avatar

Level 10

"@Reference doesn't work with new OSGI R6 annotation" -- Not TRUE!

I have never seen a situation in 6.4 when @Reference returns null. For example, in the DataSourcePool article- this annotation works perfectly to inject a DataSourcePool instance.

Adobe Experience Manager Help | Querying MySQL data using an Adobe Experience Manager 6.4 DataSource...