Writting interface in aem | Community
Skip to main content
March 15, 2025
Solved

Writting interface in aem

  • March 15, 2025
  • 3 replies
  • 560 views

Hi Team,

 

Can we write aem service without interface, what are the usecases to write aem service without interface.

 

Thanks,

Keerthi K.

Best answer by AmitVishwakarma

Hi @ns334 ,

Yes, you can write an AEM service without an interface, but whether you should depends on the use case.

 

You can define an OSGi service as a concrete class without an interface. AEM's OSGi framework allows services to be registered and consumed directly as concrete implementations.

 

Use Cases for Writing an AEM Service Without an Interface

 

1. Simple, Internal Utility Services

If the service is only used within a single component or module and doesn’t require multiple implementations, skipping the interface can reduce unnecessary abstraction.

2. No Need for Multiple Implementations

If there's no need to switch between different implementations dynamically, using a concrete class directly simplifies code maintenance.

3. Avoiding Boilerplate Code for Small Services

For very small services (e.g., a helper service with a few methods), defining an interface may not add much value.

4. Private or Final Services

If the service is meant to be used only within a specific bundle and won’t be extended or overridden, an interface might be unnecessary.

When to Use an Interface?

     - If you expect multiple implementations (e.g., different caching strategies).
     - If the service is meant to be exposed as a public API for use across multiple modules.
     - If you want to enable easy testing via dependency injection with mock implementations.

Example Without Interface

@Component(service = MyService.class) public class MyService { public String getMessage() { return "Hello from MyService!"; } }

Injected into another class like this:

@Reference private MyService myService;


Regards,
Amit Vishwakarma

 

3 replies

AmitVishwakarma
Community Advisor
AmitVishwakarmaCommunity AdvisorAccepted solution
Community Advisor
March 15, 2025

Hi @ns334 ,

Yes, you can write an AEM service without an interface, but whether you should depends on the use case.

 

You can define an OSGi service as a concrete class without an interface. AEM's OSGi framework allows services to be registered and consumed directly as concrete implementations.

 

Use Cases for Writing an AEM Service Without an Interface

 

1. Simple, Internal Utility Services

If the service is only used within a single component or module and doesn’t require multiple implementations, skipping the interface can reduce unnecessary abstraction.

2. No Need for Multiple Implementations

If there's no need to switch between different implementations dynamically, using a concrete class directly simplifies code maintenance.

3. Avoiding Boilerplate Code for Small Services

For very small services (e.g., a helper service with a few methods), defining an interface may not add much value.

4. Private or Final Services

If the service is meant to be used only within a specific bundle and won’t be extended or overridden, an interface might be unnecessary.

When to Use an Interface?

     - If you expect multiple implementations (e.g., different caching strategies).
     - If the service is meant to be exposed as a public API for use across multiple modules.
     - If you want to enable easy testing via dependency injection with mock implementations.

Example Without Interface

@Component(service = MyService.class) public class MyService { public String getMessage() { return "Hello from MyService!"; } }

Injected into another class like this:

@Reference private MyService myService;


Regards,
Amit Vishwakarma

 

Uppari_Ramesh
March 16, 2025

Hi @ns334 ,

You can create a service without interface but it is not best practise as you miss java abstraction and also flexibility of multiple implementations. Better to go with Interface.

 

Thanks

Ramesh

March 17, 2025

hi @ns334 

If the service is very simple or designed for internal use, you may choose not to create an interface. For example, if it's a single-use class with minimal logic that won’t need to be reused or tested independently - Example could setting some config values at site level to be picked in analytics as part of digital data layer.