AEM - Multiple implementation of a Sling Service

Avatar

Avatar
Validate 25
Level 3
Jai1122
Level 3

Likes

21 likes

Total Posts

127 posts

Correct reply

2 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Boost 5
Boost 3
View profile

Avatar
Validate 25
Level 3
Jai1122
Level 3

Likes

21 likes

Total Posts

127 posts

Correct reply

2 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Boost 5
Boost 3
View profile
Jai1122
Level 3

29-01-2021

Hi Experts,

  Per the accepted answer of this post,  "If you have 2 services implementing the same interface and you want always to get the first or second service implementation, then your design is flawed. You should use different service interfaces then". I would like to know the reason why this design is flawed. 

 

Kindly enlighten.

 

Regards,

Jai

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,166 posts

Correct reply

1,079 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,134 likes

Total Posts

3,166 posts

Correct reply

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

30-01-2021

When you control the service interface and the implementations, it hardly makes sense to have 2 distinct implementations and annotate the @reference annotation to get explicitly implementation 1 or 2.

 

If you need implementation 1, why don't you reference directly reference the implementation 1? Remember, that you can do something like that:

 

@component(service=Service1.class)

public class Service1 {

...

}

 

and use 

@reference

Service1 service;

 

in your code to reference the Service1 component directly. You should do that for services which are not publicly available (or you need to make your service class public, which is not always what you want). 

So in this case implementing an interface with 2 implementations is overhead, when you only want implementation 1 or 2.

 

But of course there are usecases, where it's absolutely the right approach to provide a service interface with 2 implementations. But these are rather corner cases, and I would not expect them to happen outside of libraries or frameworks. Sorry, maybe I should have added it to that answer as well.

 

Answers (1)

Answers (1)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

29-01-2021

@Jai1122 

What @Jörg_Hoh  meant is, service user no need to worry about which implementation reference that is begin injected. Always service with highest ranking will be injected, if ranking is same oldest service will be injected(which guarantees some stability).

Writing two service implementations at a time doesn't make sense(with in same application). It should be old or new based on ranking. 

https://helpx.adobe.com/experience-manager/using/osgi_getting_started.html

Hope this answers your question.