AEM - Multiple implementation of Interface But needs to call specific implementation | Community
Skip to main content
August 21, 2024
Solved

AEM - Multiple implementation of Interface But needs to call specific implementation

  • August 21, 2024
  • 2 replies
  • 808 views

you have an interface class called Animal it has two implementation as cat and Dog, you want to use this in your sling model or service how would you make sure that the implementation you are getting should be dog only. what would be your approach?

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

Step 1-: Define the Interface and Implementations

Animal.java (Interface)

public interface Animal { void makeSound(); }

Cat.java (Implementation 1)

import org.apache.sling.api.resource.Resource; public class Cat implements Animal { @9944223 public void makeSound() { System.out.println("Meow"); } }

Dog.java (Implementation 2)

import org.apache.sling.api.resource.Resource; public class Dog implements Animal { @9944223 public void makeSound() { System.out.println("Woof"); } }

Step 2-: Use Dependency Injection in Your Sling Model.
To ensure that you get the Dog implementation specifically, you should use the @3214626 annotation and mention target instance which you want to call.

import org.apache.sling.models.annotations.Model; import org.osgi.service.component.annotations.Reference; @Model(adaptables = Resource.class) public class MySlingModel { @3214626(target = "(instance=dog)") private Animal animal; public void makeAnimalSound() { animal.makeSound(); } }

 

2 replies

NITINTR2Accepted solution
Level 2
August 21, 2024

Step 1-: Define the Interface and Implementations

Animal.java (Interface)

public interface Animal { void makeSound(); }

Cat.java (Implementation 1)

import org.apache.sling.api.resource.Resource; public class Cat implements Animal { @9944223 public void makeSound() { System.out.println("Meow"); } }

Dog.java (Implementation 2)

import org.apache.sling.api.resource.Resource; public class Dog implements Animal { @9944223 public void makeSound() { System.out.println("Woof"); } }

Step 2-: Use Dependency Injection in Your Sling Model.
To ensure that you get the Dog implementation specifically, you should use the @3214626 annotation and mention target instance which you want to call.

import org.apache.sling.models.annotations.Model; import org.osgi.service.component.annotations.Reference; @Model(adaptables = Resource.class) public class MySlingModel { @3214626(target = "(instance=dog)") private Animal animal; public void makeAnimalSound() { animal.makeSound(); } }

 

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 21, 2024

Hi, 

It depends on where you are injecting the service (assuming this is an OSGi service) with multiple implementations. If you are injecting it into a Sling Model, you should use a filter property along with the @OSGiService annotations. Please refer to this guide: OSGi Service Filters. Here is an example of how to do it: Referencing a Specific Service Implementation Inside Sling Model.

 

If you are using a servlet, then the @3214626 annotation would work as already explained. For more details, see: Register Sling Servlet or Any Service.

 

Hope this helps

 

Esteban Bustamante