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


