Liferay DXP’s runtime environment consists of services (OSGi services). The OSGi service registry and Service Component Runtime (SCR) facilitate providing and consuming services. Contexts and Dependency Injection (CDI) is a Java SE and EE standard for lifecycle events, stateful objects, and dependency injection. OSGi CDI Integration brings features and capabilities of CDI to OSGi and makes OSGi services available to CDI beans. Here you’ll learn how to
Publish CDI beans as OSGi services: Register CDI beans as services you can use to customize Liferay DXP components.
Use OSGi services in beans: Leverage any OSGi service published on Liferay DXP in any bean.
The following use cases provide more detail.
Liferay DXP extension points are implemented as OSGi services. If there’s a piece of functionality you must customize, you don’t have to learn OSGi to do it: you can write your extension/override as a CDI bean instead and use OSGi CDI integration to publish your bean as an OSGi service.
By implementing the service in your CDI bean class and adding the integration’s
@org.osgi.service.cdi.annotations.Service annotation to it, your bean
registers as providing that OSGi service. In this way, service consumers can use
your service implementation (i.e., your CDI bean).
For example, the Service Registry in figure 1 shows two implementations of an
OSGi service called
MyBeanis a CDI bean whose service rank is
MySvcImplhas a service rank of
The Service Component Runtime (
SCR) finds the matching, highest ranked
service provider and binds it to consumer
C1. The fact that
MyBean is a CDI
bean is transparent to the SCR.
Once a CDI bean is registered as a service, components can use it as they would any other OSGi service.
Liferay DXP contains many development frameworks for all of its constructs, such as Users, Sites, Documents, Comments, and the APIs for these assets are all implemented as OSGi services. When developers write new applications using Liferay’s development frameworks, new assets become available and integrated with the rest of the system. OSGi CDI integration enables your beans to access these OSGi services.
In figure 2, for example, CDI bean
SomeBean uses the OSGi CDI integration
@org.osgi.service.cdi.annotations.Reference (along with CDI
@Inject) to inject the OSGi service
UserLocalService. The Service
Component Runtime (
SCR) finds the
UserLocalService in the Service Registry
and binds it to
These are the most common use cases, but you might have more. Now you can get started using OSGi CDI integration to publish CDI beans as OSGi services!