Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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

Avatar

Level 1

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?

1 Accepted Solution

Avatar

Correct answer by
Level 2

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 {
    @Override
    public void makeSound() {
        System.out.println("Meow");
    }
}

Dog.java (Implementation 2)

import org.apache.sling.api.resource.Resource;

public class Dog implements Animal {
    @Override
    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 @reference 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 {

    @reference(target = "(instance=dog)")
    private Animal animal;

    public void makeAnimalSound() {
        animal.makeSound();
    }
}

 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 2

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 {
    @Override
    public void makeSound() {
        System.out.println("Meow");
    }
}

Dog.java (Implementation 2)

import org.apache.sling.api.resource.Resource;

public class Dog implements Animal {
    @Override
    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 @reference 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 {

    @reference(target = "(instance=dog)")
    private Animal animal;

    public void makeAnimalSound() {
        animal.makeSound();
    }
}

 

Avatar

Community Advisor

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 @reference annotation would work as already explained. For more details, see: Register Sling Servlet or Any Service.

 

Hope this helps

 



Esteban Bustamante