Expand my Community achievements bar.

SOLVED

AEM Service Interfaces

Avatar

Level 2

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

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

1 Accepted Solution

Avatar

Correct answer by
Level 5

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.

View solution in original post

1 Reply

Avatar

Correct answer by
Level 5

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.