Hello, OSGi, Part 2: Introduction to Spring Dynamic Modules

Build a service-oriented application using Spring and OSGi

1 2 3 4 5 6 7 Page 5
Page 5 of 7

Spring configuration in Spring DM

Now we're ready to create our Spring configuration files. The recommended practice in Spring DM is to break your configuration into two files, one for defining Spring beans and the other for exporting the Spring beans as OSGi services. We will also break the configuration for our sample application into two files. The first step is to create a contactdao-service.xml file in our META-INF/spring folder, as shown in Listing 6.

Listing 6. Spring context file

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

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="contactDAOService"

        class="com.javaworld.sample.osgi.spring.contact.impl.ContactDAOImpl">

    </bean>

</beans>


This simple Spring context file defines the contactDAOService, pointing to the com.javaworld.sample.osgi.spring.contact.impl.ContactDAOImpl class.

Next, we create the META-INF/spring/contactdao-osgi.xml file, which will be used to export the contactDAOService object as an OSGi service:

Listing 7. contactdao-osgi.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
    <osgi:service id="contactDAOOSGiService" ref="contactDAOService"
        interface="com.javaworld.sample.osgi.spring.contact.ContactDAO">
    </osgi:service>
</beans>

The contactdao-osgi.xml file has only one <service> element, which is used to export Spring beans as OSGi services in a common registry. The service <service> [element] must have two attributes at minimum: One is the id attribute with a value equal to the name of the Spring bean to be exported. The other is the interface attribute, whose value should be equal to the interface name under which the service should be exported. (See the Spring Dynamic Modules Reference Guide for a complete list of attributes supported by the <service> element.)

So now our ContactDAO service is ready to go. The next step is to extend the HelloWorld application so that it can consume the new service.

HelloWorld as a consumer

If we want the simple HelloWorld application to act as a consumer, we have to empower it to do so. The first step is to change the bundle's MANIFEST.MF file by adding an Import-Package statement, like this:

Import-Package: com.javaworld.sample.osgi.spring.contact

The HelloWorld bundle will now be able to access classes exported from the com.javaworld.sample.osgi.spring.contact package of the ContactDAO bundle.

Next, we change the HelloWorld.java class, as shown in Listing 8.

Listing 8. Changes to HelloWorld.java

public class HelloWorld {
    ContactDAO contactDAO;
    public ContactDAO getContactDAO() {
        return contactDAO;
    }
    public void setContactDAO(ContactDAO contactDAO) {
        this.contactDAO = contactDAO;
    }
    public void start() throws Exception {
        System.out.println("Hello Spring World!! " );
        System.out.println(contactDAO.getContactList()  );
    }
    public void stop() throws Exception {
        System.out.println("Goodbye Spring World!!");
    }
}


The first thing we did in Listing 8 was to add ContactDAO as a Java bean property, with all the corresponding getter and setter methods. Next, we changed the class's start() method to call the getContactList() method of the ContactDAO service, in addition to printing out the "Hello Spring World!!" message.

1 2 3 4 5 6 7 Page 5
Page 5 of 7