Snippets / HelloWorldComponent aQute - Software Consultancy
Search
*

Hello World as Declarative Service Component

Purpose

Show the simplest bundle that uses components/declarative services.

Prerequisites

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

Copyright 2006 aQute SARL, All Rights Reserved