Over a million developers have joined DZone.
Platinum Partner

Book Giveaway & Exclusive Chapter: Modular Java

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

DZone and Pragmatic Bookshelf have partnered to bring you this exclusive chapter from 'Modular Java' (by Craig Walls). This excerpt was extracted from Modular Java, published in July 2009 by Pragmatic Bookshelf. It is being reproduced here by permission from Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, visit the Modular Java homepage.

To get started with Spring-DM, we’ll need to add these bundles to our project:

dwmjs% pax-import-bundle -g org.springframework.osgi -a spring-osgi-extender \
? -v 1.2.0 -- -DimportTransitive -DwidenScope
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8 seconds
[INFO] Finished at: Fri Mar 20 15:33:34 CDT 2009
[INFO] Final Memory: 9M/18M
[INFO] ------------------------------------------------------------------------

Here we’ve asked Pax Construct to add version 1.2.0 of the Spring-DM extender bundle (identified with a group ID of org.springframework.osgi and an artifact ID of org.springframework.osgi.extender) to the project. In
addition to the Spring-DM extender bundle itself, we’ve also asked that pax-import-bundle also pull in transitive dependencies (-DimportTransitive) and to consider all compile and runtime dependencies as potential bundles

The Spring-DM bundles are now in place and are ready to help us declaratively publish the index service.

Declaring Services

The first step in declaring a service in Spring-DM is to wire a bean in the Spring application context. In Spring, a bean is any object (not necessarily a JavaBean) that is instantiated and managed by the Spring Framework. A common way of configuring the beans that Spring creates is to define a Spring application context in an XML file. For example, consider this Spring configuration XML (index-context.xml) that we’ll use to define an application context for the index service bundle:

<beans xmlns="http://www.springframework.org/schema/beans"
<bean id="indexService"
class="dwmj.index.internal.IndexServiceImpl" >
<constructor-arg ref="compass" />
<compass:compass name="compass" >
<compass:file path="/tmp/dudeindex" />
<compass:class name="dwmj.domain.JarFile"/>

<compass:session id="compassSession" />

Here we’ve declared two beans. The first is defined with the <bean> element. This bean tells Spring to create an instance of IndexServiceImpl and to give it an ID of indexService. What’s especially interesting about this bean is that we’re telling Spring to instantiate it by calling a singleargument constructor and passing in a reference to another bean. Specifically, Spring should construct IndexServiceImpl with a reference to a bean whose ID is compass.

That brings us to the second bean. Instead of using a generic <bean> element to declare the compass bean, we’re using elements from a Compass-specific configuration namespace provided as part of the
Compass library. Ultimately, this declaration creates a bean of type org.compass.core.Compass, suitable for the first argument of the IndexServiceImpl constructor.

As mentioned before, Spring-DM creates an application context by reading all XML files in the META-INF/spring directory. Since we’re building the bundle using Maven, we’ll need to place index-context.xml in the src/main/resources/META-INF/spring directory of the index bundle project. But it won’t be alone. In addition to the core Spring configuration file, we’ll also create a separate Spring configuration file (index-osgi.xml) that tells Spring-DM to publish the indexService bean to the OSGi service registry:

<beans:beans xmlns="http://www.springframework.org/schema/osgi"
<service ref="indexService"
interface="dwmj.index.IndexService" />

Click here to download the entire exerpt.

Copyright © 2009 Craig Walls. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}