Skip to content

CDI OSGi Programming model

sauthieg edited this page Jan 30, 2011 · 2 revisions

Description of the CDI/OSGi programming model

Proposition du be refined

@Inject usage

Service injection

Have a single service dependency injected into a field. Notice that the lifecycle of this bean instance is tied to the service availability.

@Inject
@Service
private IService service;

Have a single optional service dependency injected into a field. Notice that the Instance accessor methods may return null (depending on service availability).

@Inject
@Service
private Intance<IService> service;

Filtered service declaration

@Inject
@Service
@Filter("(name=value)") // Filtered service
private IService service;

@Observes usage

This is an event based model usage. Notice that managed bean instance lifecycle is not tied to dependency availability.

Observable types

ServiceArrival

Notification sent when an observed service arrive.

ServiceDeparture

Notification sent when an observed service leave the stage.

ServiceUpdated

Notification sent when an observed service has its propertie changed.

public void bindService(@Observes ServiceArrival event) {
  event.getServiceReference();
  event.getInstance(); // Instance as we don't have a clue on the type
}
public void bindService(@Observes
                        @Specification(IService.class)
                        ServiceArrival event) {
  event.getServiceReference();
  event.getInstance(); // Instance
  // As service is dynamic, I suggest that Instance() may return null on get()
}
public void bindService(@Observes
                        @Filter("(name=value)")
                        @Specification(IService.class)
                        ServiceArrival event) {
// Filtered service dependency
}