AEM Service Interfaces | Community
Skip to main content
Level 2
September 11, 2019
Solved

AEM Service Interfaces

  • September 11, 2019
  • 1 reply
  • 2088 views

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

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

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 Umesh_Sondhi

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.

1 reply

Umesh_Sondhi
Umesh_SondhiAccepted solution
Level 4
September 11, 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.