AEM Service Interfaces

Avatar

Avatar

rohand104

Avatar

rohand104

rohand104

11-09-2019

Why do we need an interface for every service in AEM?

Can we use @reference or @inject services without an interface?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Umesh_Sondhi

MVP

Avatar

Umesh_Sondhi

MVP

Umesh_Sondhi
MVP

11-09-2019

Hi

Using interfaces is a good practice to decouple the user of a service from the implementation. In many cases you even want to have an API bundle so the user of the service does not need a maven dependency to the implementing bundle.On the other hand, you are not required to use interfaces. Especially when u wire components inside a bundle interfaces,it is often an unnecessary layer. In this case simply export the service directly with the class.

@Component(service = AbcService.class)

public class AbcService {
...
}

for client code

@Reference
private AbcService abcService;

So the difference is that you have to specify the service property if you want to export a component with its implementation class.

Answers (0)