Why do we need an interface for every service in AEM?
Can we use @reference or @inject services without an interface?
Solved! Go to Solution.
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.
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.
Views
Likes
Replies