concept OSGi platform in category spring

appears as: OSGi platform, OSGi platform, OSGi platforms, The OSGi platform, n OSGi platform, OSGi platforms
Spring Dynamic Modules in Action

This is an excerpt from Manning's book Spring Dynamic Modules in Action.

Dependency injection is a simple yet rather powerful pattern. Combined with interface-based programming, it allows you to write more testable and less tightly coupled code. Component management (creation and wiring) can then be delegated to lightweight containers, such as Spring. The Spring Framework can be a great help in your OSGi development: the lightweight container assembles the inner elements of your modules, and Spring DM will take over to make them interact easily with the OSGi platform.

Spring DM brings new powers to your OSGi bundles; namely, all the power available in the Spring Framework, which means a great deal! We saw in the previous chapter how Spring DM can automatically create an application context on behalf of a bundle, assuming this bundle contains Spring configuration files in a specific location. With the availability of the Spring lightweight container, a bundle can build and wire all of its constituent components and let Spring DM interact with the OSGi platform to register Spring beans in the service registry. All this can be driven declaratively, without any of your Java code depending on the OSGi APIs.

For example, you can make a reference to any already installed bundle in the OSGi platform by using the symbolic-name attribute, just as in the following snippet, which retrieves the Spring DM extender bundle directly:

<osgi:bundle
id="springDmExtenderBundle"
symbolic-name="org.springframework.osgi.extender" />

The bundle element is not limited to only Spring DM bundles; it works for any OSGi-compliant bundle. You can also use the bundle element with bundles that aren’t yet deployed to the OSGi platform, thanks to the location attribute. With this attribute, you can indicate where to find a bundle by using the Spring resource syntax. You can also choose an action to be performed when the bundle is loaded by Spring DM. The following snippet shows how to create a bundle bean from the filesystem and then start it:

A Spring-powered bundle doesn’t have to worry about its application context’s creation and destruction; the Spring DM extender handles this process on behalf of the bundle. Nevertheless, it’s useful to have some knowledge of the application context lifecycle because it can have implications for the OSGi platform itself. In this section, we’ll describe Spring DM’s default behavior for handling application contexts. We’ll also describe why you might want to override the default behavior, and how.

Figure 10.5. When running an integration test, Spring DM bootstraps an OSGi platform for the duration of the test. The test class is turned into an OSGi bundle on the fly and is used to provision the platform. By default, Spring DM provisions the platform with bundles taken from the Maven 2 local repository.