Chapter 3. Implement Simple Spring-DM Service

In this chapter we will implement our first Spring-DM service.

Before we create our first Spring-DM project let's define a use case for it


Our project will consist of a simple service which will define a method that accepts a String and returns it back in reverse order. We will also export this service as an OSGi™ service, so later on when we develop Spring-DM web-based project we will be able to reference this service as plain OSGi™ service using features of Spring-DM. 


We are going to be using Eclipse PDE as it is a natural fit for OSGi (Spring-DM included) based projects.


3.1. Create Spring-DM service Project

Create Plug-in Project

Click on: File -> New -> Project


New Project window will open up: 



Select Plug-in Project -> Next



Enter org.springframework.osgi.springdm.simple as Project Name.

Select an OSGi Framework [Equinox] 


Click Next



Change Plug-in Name to Simple Service

Click Finish

Remember this procedure since you might want to reuse it while creating other projects.

We are finished creating project structure. Now we are ready to write some code. . .

3.2. Implement Spring-DM service

Create the following Java Artifacts: – service interface:

package org.springframework.osgi.sample.service;

publicinterface StringReverser {

public String reverse(String str);
==================================================================================== – service implementation:

package org.springframework.osgi.sample.service.impl;

import org.apache.log4j.Logger;
import org.springframework.osgi.service.StringReverser;

publicclass StringReverserImpl implements StringReverser {
privatestatic Logger logger = Logger.getLogger(StringReverserImpl.class);

public String reverse(String str) {"Receiveing string: " + str);
char[] chars = str.toCharArray();
StringBuffer buf = new StringBuffer();

for (int i = chars.length - 1, x = 0; i >= 0; i--, x++) {
String reveresed = buf.toString();"Reterning string: " + reveresed);
return reveresed;

This is it. As you can see, it is a very simple POJO based service.

Your project should look like this:



3.3. Configure Spring-DM servie

In order for this project to be recognized as an OSGi™ project it should have MANIFEST file. We could have created this project originally as Eclipse PDE project (similar to the way we did Log4J project) and MANIFEST would be created for us automatically; however for the purposes of learning experience we are going to define it manually. It will also allow us not to be coupled completely to the environment provided by the Eclipse PDE (just use it enough to help us out)

Create META-INF directory at the root of the project

Create MANIFEST.MF file inside of META-INF directory (i.e., META-INF/MANIFEST.MF)

Copy the following contents into MANIFEST file:

Manifest-Version: 1.0
Bundle-Version: 1.0
Bundle-SymbolicName: org.springframework.osgi.simple.reverser
Bundle-Name: Simple String Reverser Bundle
Bundle-Vendor: Spring Framework
Bundle-ManifestVersion: 2
Export-Package: org.springframework.osgi.sample.service
Import-Package: org.apache.log4j;version="1.2.15"

As you see, we are exporting the package which contains our service definition (interface). We are also importing log4j package, as we depend on it within our code.

Now we have enough for Equinox (our OSGi platform) to recognize our project as valid OSGi bundle.

However few things are still missing. . .

For our project being Spring based project, we have yet to define our Application Context and its configuration. We can obviously define it anywhere, but then we would have to let Spring DM Extender know where the configuration files located. Chapter 4.1. Bundle format and Manifest headers explains in details how to do that (extra MANIFEST headers). But for the purposes of this tutorial we are going rely on default location specified and expected by Spring DM. This way Spring DM Extender bundle will be able to find our Application Context configuration , bootstrap it (similar to the way ContextLoaderListener does in web environment) and export our service as OSGi™ service into our Target Platform without any extra MANIFEST headers.

Create spring directory inside of META-INF directory. This is the default directory where Spring DM Extender bundle will look for Spring configurations.

Create simpleservice.xml file and copy the following contents into the file:

<?xml version="1.0" encoding="UTF-8"?>



This file configures our service as a regular Spring bean

Create simpleservice-osgi.xml file and copy the following contents into the file:

<?xml version="1.0" encoding="UTF-8"?>



This file uses osgi name space to provide a directive for Spring DM’s Extender bundle to export our service and publish it under the interface it implements.

This is it. Your META-INF directory should look like this:




The only thing that is left to do is to package our bundle and deploy it. However, Eclipse PDE environment already recognizes our project as valid OSGi project and included (deployed) it as a Workspace resource into our Target Platform, which is very convinient during an active development (see below)

And even though we will need to package it later for distribution or integration testing, we don’t have to do it just yet.

3.4. Deploy Spring-DM Service

As I mentioned before, during the active development there is no need for explicite packaging or deployment. Simply start the Spring DM Target Platform and you’ll see that the bundle is deployed and ACTIVE


Type ss on the console and you'll see:


. . . . . . .
35    ACTIVE      org.springframework.osgi.springdm.simple_1.0.0
. . . . . . .


If you enable Eclipse PDE tracing (debug/services) as described in the previous chapter, you can easily find similar message in the console:

. . . . . . .
registerService[initial@refere../../org.springframework.osgi.springdm.simple/ [18]]
. . . . . . .

There you can clearly see that our service was exported successfully into OSGi environment.

3.5. Package Spring-DM Service

Packaging our service is also a very simple task. We are going to use Eclipse PDE environment to do it the same way if we were to package a regular Eclipse Plug-in.


Right-click on our project -> Export

Export window will open up:

Select Deployable Plug-ins and Fragments -> Next



Make sure our project is selected amongst the Available Plug-ins and Fragments

For the Directory (where we want the JAR to be exported) select the absolute path for the root of the project 

Click Finish and then Refresh the project

You'll see that new directory plugin was created and our JAR file was exported there.

Remember this procedure since you might want to reuse it while creating other projects.

Our project is complete and packaged as valid Spring DM/OSGi bundle.