Hello World as Declarative Service Component
Purpose
Show the simplest bundle that uses components/declarative services.
Prerequisites
- Declarative services - org.osgi.service.component
- Log Service - org.osgi.service.log
- osgi interfaces, provides all service interfaces for the OSGi specifications.
Instructions
A component is a POJO (plain old Java object) that is created by the declarative services runtime when the bundle is started and its references are satisfied. References are dependencies on other services. In this example, we have one reference: the Log Service. We create a static dependency on the log service; this means that the setLog() method has been called before we are activated. If the Log Service disappears, we will be deactivated.
Source: aQute.hellocomponent.HelloComponent package aQute.hellocomponent; import org.osgi.service.component.*; import org.osgi.service.log.*; public class HelloComponent { LogService log; protected void activate(ComponentContext context) { log.log(LogService.LOG_INFO, "Hello Component"); } protected void deactivate(ComponentContext context) { log.log(LogService.LOG_INFO, "Goodbye Component"); } public void setLog(LogService log) { this.log = log; } } Bnd: aQute.hellocomponent.bnd Export-Package: aQute.hellocomponent, org.osgi.service.log Service-Component: aQute.hellocomponent.HelloComponent; \
log=org.osgi.service.log.LogService
Links
- The specification discusses declarative services in chapter 112 of the OSGi Compendium.
- Declarative Services Javadoc
- Log Service Javadoc
- aQute.hellocomponent.jar


