Applications that take advantage of the OSGi capabilities of the SpringSource Application Platform are typically comprised of multiple bundles. Each bundle may have dependencies on other bundles. Furthermore, each bundle exposes only certain packages and services. In this chapter, we look at how to create bundles, import and export appropriate functionality, and create artifacts to deploy web application on the SpringSource Application Platform.
![]() | Tip |
|---|---|
This is an abbreviated introduction to OSGi bundles. Please refer to the Spring Dynamic Modules for OSGi documentation for full details. |
An OSGi bundle is simply a jar file with metadata that describe additional characteristics such as version and imported and exported packages.
A bundle exports types and services to be used by other bundles:
Types, through
Export-Package
directive,
Services, through Spring DM
<service> element.
A bundle may import types and services exported by other bundles:
Types, through Import-Package directive,
Services, through Spring DM <reference> element.
Let's see an example from the hotel booking sample application. The following listing shows the MANIFEST.MF
file for the org.springframework.booking.service.internal bundle.
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Hotel Booking Service Implementation Bundle-SymbolicName: org.springframework.booking.service.internal Bundle-Vendor: SpringSource Inc. Import-Package: org.springframework.booking.domain, org.springframework.booking.service, org.springframework.booking.repository.user, org.springframework.booking.repository.hotel Export-Package: org.springframework.booking.service.internal Import-Library: org.springframework.spring;version="2.5.4", org.hibernate;version="3.2.6.ga"
The org.springframework.booking.service.internal bundle expresses its dependencies on
the org.springframework.booking.domain, org.springframework.booking.service,
org.springframework.booking.repository.user,
and org.springframework.booking.repository.hotel packages. It also
exposes the org.springframework.booking.service.internal package.
The bundle also imports version 2.5.4 (or later) of the org.springframework.spring library
since it uses Spring types in its implementation (we will examine the details of the library artifact in
Section 5.3, “Simplifying dependencies through libraries” section). It also imports version 3.2.6.ga (or later) of the org.hibernate library
for reasons described in Section 8.1, “Working with Hibernate”).
Note that you do not specify the bundle that will provide the imported packages. The SpringSource Application Platform will examine the available bundles and satisfy the required dependencies.
The following osgi-context.xml file for the same bundle declares services exported by the bundle and references to services exported by other bundles.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<service ref="bookingService"
interface="org.springframework.booking.service.BookingService" />
<reference id="transactionManager"
interface="org.springframework.transaction.PlatformTransactionManager" />
<reference id="userRepository"
interface="org.springframework.booking.repository.user.UserRepository" />
<reference id="hotelRepository"
interface="org.springframework.booking.repository.hotel.HotelRepository" />
</beans:beans>
The service element exports the bookingService bean
(a regular Spring bean declared in the module-context.xml file) and specifies
org.springframework.booking.service.BookingService as the type
of the exported service. The reference elements define various beans
as references to services exported by other bundles.