Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

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

Avatar

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

471 likes

Total Posts

1,039 posts

Correct Reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

471 likes

Total Posts

1,039 posts

Correct Reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile
Veena_Vikram
MVP

13-03-2019

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

Replies

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,118 likes

Total Posts

3,149 posts

Correct Reply

1,073 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,118 likes

Total Posts

3,149 posts

Correct Reply

1,073 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

14-03-2019

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

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,358 likes

Total Posts

3,228 posts

Correct Reply

918 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,358 likes

Total Posts

3,228 posts

Correct Reply

918 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

14-03-2019

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.

Avatar

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

471 likes

Total Posts

1,039 posts

Correct Reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

471 likes

Total Posts

1,039 posts

Correct Reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile
Veena_Vikram
MVP

14-03-2019

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

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,358 likes

Total Posts

3,228 posts

Correct Reply

918 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,358 likes

Total Posts

3,228 posts

Correct Reply

918 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

14-03-2019

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.

Avatar

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

471 likes

Total Posts

1,039 posts

Correct Reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

471 likes

Total Posts

1,039 posts

Correct Reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile
Veena_Vikram
MVP

15-03-2019

Oh okay.

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,118 likes

Total Posts

3,149 posts

Correct Reply

1,073 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,118 likes

Total Posts

3,149 posts

Correct Reply

1,073 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

15-03-2019

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

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

16-03-2019

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