Previous Topic

Next Topic

Book Contents

Book Index

Providing a Service

A document illustrating the steps for creating a component, which provides a service.

To create a component that provides a service, you have to go through the following stages:

  1. Write the service interface.

    The following example is the HelloService interface:

    package simple.service;

    public interface HelloService {
        
      public void hello();
    }

  2. Write the service interface implementation class which will be the component's implementation class as well:

    Here is the HelloService implementation class:

    package simple.service.impl;
    import simple.service.HelloService;

    public class HelloServiceImpl implements HelloService {

      public void hello() {
                
         System.out.println("Hello components!");
      }
    }

  3. Write the component description.

    The XML description of the HelloServiceComponent is the following.

    <?xml version="1.0" encoding="UTF-8"?>
    <scr:component name="HelloServiceComponent"
    xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
      <!-- The component's implementation class-->
      <implementation class="simple.service.impl.HelloServiceImpl"/>
      <service>
         <!--The interface of the service provided by the component-->
         <provide interface="simple.service.HelloService"/>
      </service>  
    </scr:component>