If you've had your ear to the ground recently, you've likely been hearing a lot about service-oriented application development with OSGi and Spring Dynamic Modules. In this second article in the Hello, OSGi series, find out why Spring DM is such an exciting option for developers already familiar with Spring configuration, who want to exploit OSGi's modularity, easy versioning, and application lifecycle support.
OSGi, also known as the Dynamic Module System for Java, specifies a modular approach to Java application development, as well as a set of standardized methods for managing dependencies between modules. The Spring Dynamic Modules for OSGi Service Platforms project (Spring DM) allows you to build Spring applications that can be deployed in an OSGi container. For Java enterprise developers familiar with the Spring programming and configuration model, Spring DM is an easier way to become familiar with OSGi's modular approach to application development. In addition to enabling Spring developers to access OSGi's modular framework and dynamic configuration, Spring DM provides the low-level coding required by most OSGi applications, so that you can focus on your application's business logic.
The first article in this series ("Hello, OSGi, Part 1: Bundles for beginners") introduced a standard approach to OSGi development using the OSGi API and the open source Equinox container implementation. You learned about the OSGi architecture, especially containers and bundles, and had your first experience developing an OSGi-based Hello World application. The application example did not run very deep because the goal was simply to grasp the fundamentals of OSGi.
In this article, you'll build another Hello World application, this time using the Spring DM framework. You'll learn what Spring DM is and how it leverages OSGi's separation of application logic into modules, as well as its runtime enforcement of module boundaries. You'll also learn how to use Spring DM to do the following:
- Dynamically install, update, and uninstall modules in a running system.
- Build service-oriented applications (SOAs) by dynamically discovering and using services provided by other modules in a system.
- Use Spring's
DataSourceclass to instantiate, configure, assemble, and decorate components within and across system modules.
As you'll see, using Spring DM will free you of some of the rote work, and even the harder work of understanding what is happening beneath the hood with OSGi. As a result, you'll be able to get deeper into your application logic, and do it sooner in the development process.
To follow the examples in this article you will need a development environment consisting of Eclipse 3.3 and Spring Dynamic Modules. For the final exercise you will also need an RDBMS such as Apache Derby. You'll find more below about setting up your Spring DM development environment in Eclipse 3.3.
OSGi and the Spring framework
Currently, OSGi's popularity is very much on the rise. Several application servers have been repackaged to leverage OSGi's modular framework, including IBM's WebSphere Application Server, BEA's microService Architecture (mSA), and JOnAS 5, an open source application server built from the ground up on an OSGi architecture. JBoss has also recently announced its work on an OSGi-based classloader and its intention to create an OSGi core specification implementation. Perhaps most importantly, the Eclipse Foundation's OSGi container/runtime component engine, Equinox, has recently been elevated to top-level project status, where it will serve as the basis for the new Eclipse Runtime Initiative.
Even before OSGi achieved its current surge in popularity, there was talk of combining it with Spring. Eventually, this talk led to the Spring Dynamic Modules for OSGi Service Platforms project. The functionality of Spring DM can be divided into two main components: First, it provides Spring framework JARs in the form of OSGi bundles. As you know from the previous article in this series, OSGi bundles are nothing but Java Archive (JAR) files that contain additional entries in a
META-INF/MANIFEST.MF file, which acts as the deployment descriptor for an OSGi bundle. (Note that you cannot deploy a JAR file directly when using OSGi; you need to package it using the OSGi bundle format.)
Second, Spring DM provides three OSGi-specific Spring bundles/JARs:
Applications built using Spring DM are built differently from those that use the Spring framework. When you build a Spring application, you define your configuration information in one or more Spring configuration files, which are mostly XML files. The Spring framework uses these config files to create an application-context object at application startup. After the application context is created it is used to instantiate, configure, assemble, and decorate objects within the application.