Book Giveaway & Exclusive Chapter: Modular Java
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] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8 seconds
[INFO] Finished at: Fri Mar 20 15:33:34 CDT 2009
[INFO] Final Memory: 9M/18M
[INFO] ------------------------------------------------------------------------
dwmjs%
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
(-DwidenScope).
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:compass="http://www.compass-project.org/schema/spring-core-config"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.compass-project.org/schema/spring-core-config
http://www.compass-project.org/schema/spring-compass-core-config-2.0.xsd">
<bean id="indexService"
class="dwmj.index.internal.IndexServiceImpl" >
<constructor-arg ref="compass" />
</bean>
<compass:compass name="compass" >
<compass:connection>
<compass:file path="/tmp/dudeindex" />
</compass:connection>
<compass:mappings>
<compass:class name="dwmj.domain.JarFile"/>
</compass:mappings>
</compass:compass>
<compass:session id="compassSession" />
</beans>
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"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd">
<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.
| Attachment | Size |
|---|---|
| DeclaringServices.pdf | 149.49 KB |
| bookcontest-modjava-sidways.jpg | 29.37 KB |
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Stevi Deter replied on Tue, 2009/09/15 - 10:52am
Sudhakar Ramasamy replied on Tue, 2009/09/15 - 12:16pm
Mike P(Okidoky) replied on Tue, 2009/09/15 - 12:41pm
Slava Ustovitskiy replied on Tue, 2009/09/15 - 10:09pm
Neil Shannon replied on Wed, 2009/09/16 - 8:16am
Lyndsey Clevesy replied on Wed, 2009/09/16 - 10:25am
in response to:
Mike P(Okidoky)
Hi Mike,
You may comment on your experiences with this book, or you can comment on your experiences with the topic of the book. Or you may comment on this particular chapter.
Thanks!
Lyndsey
Jose Fernandez replied on Wed, 2009/09/16 - 10:48am
Dan Dromereschi replied on Thu, 2009/09/17 - 1:23am
Paul Cowan replied on Thu, 2009/09/17 - 3:46pm
Expert Writer replied on Sat, 2009/09/19 - 1:38am
Hi,
Nice work! I personally like the Pragmatic bookshelf. I will be looking for more information and details
<a href="http://www.topcustomessays.co.uk"> Essays</a>
Christian Voller replied on Thu, 2009/09/24 - 5:50am