In Adobe Experience Manager (AEM), the @Reference
annotation is used in Java code to define a reference to another component or service in AEM. It is part of the AEM Component Development Kit (CDK) Annotations, which are a set of annotations that help developers define the behavior and properties of AEM components.
The @Reference
annotation is used to inject a reference to a service or a component into a Java class in AEM. It allows components or services to be dynamically bound or unbound at runtime, providing a way for components to interact with each other or with services in a loosely coupled manner.
The @Reference
annotation can be used to declare dependencies on various types of resources, such as OSGi services, Sling models, AEM services, and other AEM components. It provides a simple way to specify the relationship between components and services, and allows for easy management of dependencies in AEM applications.
Here’s an example of how the @Reference
annotation can be used in AEM:
import org.osgi.service.component.annotations.Reference; import com.adobe.cq.wcm.core.components.services.ImageHandler; public class MyComponent { @Reference private ImageHandler imageHandler; // ... // Method that uses the injected ImageHandler service public void myMethod() { // Use the imageHandler service here } // ... }
In this example, the ImageHandler
service is injected into the MyComponent
Java class using the @Reference
annotation. The injected service can then be used within the class to perform operations related to image handling. Note that the actual implementation of the ImageHandler
service will be provided by an OSGi bundle or a Sling model registered in the AEM instance.
The Reference Policy
The reference policy determines how the AEM platform should behave when the referenced service becomes unavailable or is replaced by a new service. There are several reference policies available, including:
@Reference(policy = ReferencePolicy.STATIC)
: This is the default reference policy. It means that the referenced service is injected once when the component is activated, and it is not updated if the service is replaced.@Reference(policy = ReferencePolicy.DYNAMIC)
: This policy allows the referenced service to be injected and updated dynamically at runtime. If the referenced service becomes unavailable, the AEM platform will try to find a replacement service.@Reference(policy = ReferencePolicy.EVENT)
: This policy allows the component to receive notifications when the referenced service becomes available, updated, or unavailable. This policy requires the component to implement theorg.osgi.service.event.EventHandler
interface.@Reference(policy = ReferencePolicy.GREEDY)
: This policy means that the component will not be activated until all of its referenced services are available. If any referenced service becomes unavailable, the component will be deactivated.
The Reference Cardinality
When using the @Reference
annotation, you can specify the cardinality of the reference to determine how many instances of the service the component requires.
There are several cardinalities available in OSGi, including:
@Reference(cardinality = ReferenceCardinality.MANDATORY)
: This is the default cardinality. It means that the component requires exactly one instance of the referenced service. If no instance of the service is available, the component will fail to activate.@Reference(cardinality = ReferenceCardinality.OPTIONAL)
: This cardinality means that the component does not require the referenced service to be available. If an instance of the service is available, it will be injected into the component. If no instance of the service is available, the component will still activate.@Reference(cardinality = ReferenceCardinality.MULTIPLE)
: This cardinality means that the component requires multiple instances of the referenced service. All available instances of the service will be injected into the component.@Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE)
: This cardinality means that the component requires at least one instance of the referenced service. If no instance of the service is available, the component will fail to activate.