annotations for aem








As many of you know, we are now already out of SCR annotations and moving swiftly into OSGI Declarative services[Post AEM 6.2]. In this blog we are going to discuss only about the OSGI declarative services annotations [R7] which can be used in different scenarios.

All the Annotations below can be used directly in the places mentioned. So happy and fast coding 😁

Note: Before we start using the annotations, we need to have the dependencies added in the pom (pre-requisite of course 😉)







Servlet Annotation:

Servlet Method — GET via path: @component(service = Servlet.class, property = {
Constants.SERVICE_DESCRIPTION + “=Sample GET Servlet”,
“sling.servlet.methods=” + HttpConstants.METHOD_GET, “sling.servlet.paths=” + “/bin/sampleServlet” })
Servlet Method — POST via path: @component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + “=Sample POST Servlet”, “sling.servlet.methods=” + HttpConstants.METHOD_POST, “sling.servlet.paths=” + “/bin/sampleServlet” })
Servlet Method — GET via resource type: @component(service=Servlet.class, property={ Constants.SERVICE_DESCRIPTION + “=Sample Demo Servlet”, “sling.servlet.methods=” + HttpConstants.METHOD_GET, “sling.servlet.resourceTypes=”+”sampleproject/components/structure/page”, “sling.servlet.selectors=” + “json”})
Servlet Method — POST via resource type: @component(service=Servlet.class, property={ Constants.SERVICE_DESCRIPTION + “=Sample Demo Servlet”, “sling.servlet.methods=” + HttpConstants.METHOD_POST, “sling.servlet.resourceTypes=”+”sampleproject/components/structure/page”, “sling.servlet.selectors=” + “json”})
OSGI Service: An OSGI Service is used to fetch the values authored at configuration level

Service Impl Annotations:

@component(service = SampleService.class, immediate = true, enabled = true)
@Designate(ocd = SampleServiceConfiguration.class)

Here @Designate – defines at class level the object-class-definition (where configuration values are defined).

OSGI Configuration Annotations: This SampleServiceConfiguration.class will be used @Designate in above service

Class level annotation: @ObjectClassDefinition( name = “Sample Configuration”, description = “This configuration reads the values from sample OSGI Configuration”)
Attribute level annotation: @AttributeDefinition( name = “Enable config”, description = “This property indicates to enable the sample configuration”, type = AttributeType.BOOLEAN)
Scheduler Annotations: The scheduler performs an AEM job at scheduled run time. It implements runnable interface.

@component(service = SampleScheduler.class, immediate = true)
@Designate(ocd = SampleSchedulerConfiguration.class)

public class SampleScheduler implements Runnable {}

More Scheduler Annotations:

@activate: Activate annotation is used to initialize stuff.
@deactivate: Deactivate annotation deactivates the scheduler
@MODIFIED: Modified annotation is used to perform some actions on modification of the scheduler configuration

Event Handler Annotations: An event handler responds to sling level events and it implements EventHandler interface

@component(service = EventHandler.class,
immediate = true,
property = {
EventConstants.EVENT_TOPIC + “=org/apache/sling/api/resource/Resource/*”

@ServiceDescription(“Demo for Event Handler”)

public class SampleEventHandler implements EventHandler{}

Event Listener Annotations: An event listener responds to events that occur at the JCR level. For example:

A node was added
A node was moved
A node was deleted
A property was added to a node
A property was changed
A property was deleted
The event listener class implements EventListener interface

@component(service = EventListener.class, immediate = true)

public class SampleEventListener implements EventListener{}

Workflow Annotations: The workflow class implements WorkflowProcess interface

@component(service = WorkflowProcess.class, property = { “process.label = Sample Workflow” })

public class SampleWorkflow implements WorkflowProcess{}

Model Annotations: Sling Models are “pure” POJOs that maps Sling objects (resources, request objects, etc.)

Model class Annotations: @Model Annotation is used to specify the class is a sling model.

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

defaultInjectionStrategy: The injection strategy of the fields

Model Attributes Annotations:

@inject: Fields that are supposed to be injected are annotated using @inject Annotation. OSGi Services can also be injected.

@Named: If the field or method name doesn’t exactly match the property name, @Named can be used

@default: To provide a default value to the fields, this annotation can be used

@PostConstruct: This annotation can be used to run the logic once all the field level injections are done.

There are many more annotations available but I have listed the major ones that are used frequently. Please feel free to drop a comment if you want me to add more to the list.

If you like the content, please like and share the post for better reach. Have a nice day 😘.


Read Full Blog

Annotations for AEM



Please use this thread to ask the related questions.












Please ensure to post the contents to the appropriate section of the community. This section should be used to submit feature requests for the product. This will be removed in a day or two due to the mentioned reason so please make sure to move this to Q&A section before its removal.

Status changed to: Duplicate








Sure @hamidk92094312, thanks for the info.



Kiran Vedantam.