Spring DM - OSGi with the Spring Framework

butterbeanspipeSoftware and s/w Development

Jul 14, 2012 (5 years and 2 months ago)

400 views

Baden Basel Bern Brugg Lausanne Zürich Düsseldorf Frankfurt/M.
Freiburg i. Br. Hamburg München Stuttgart Wien



Spring DM
-
OSGi with Spring Framework
Patrick Baumgartner
AD Consultant
patrick.baumgartner@trivadis.com
Zürich, 10.11.2009
© 200
9



About me

Application Development Consultant

Web development with Spring Framework

OSGi with Spring DM & Spring Framework

Agile Software Development

Certified ScrumMaster

Co
-
Author of "OSGi in
der
Praxis“
Spring Dynamic Modules
-
OSGi with Spring Framework
2
© 200
9



Daten sind
immer im Spiel.
Agenda

What is OSGi?

The hard
-
coding Way

Declarative Services

Spring DM

Blueprint Services

Demo

Conclusion
Spring Dynamic Modules
-
OSGi with Spring Framework
3
© 200
9



What is OSGi?

The OSGi framework is a module system for Java that
implements a complete and dynamic component model,
something that does not exist in standalone Java/VM
environments. […]
(Source: Wikipedia)
Spring Dynamic Modules
-
OSGi with Spring Framework
4
© 200
9



Bundle Manifest

A special JAR
Spring Dynamic Modules
-
OSGi with Spring Framework
5
Image Source:
http://www.handycandy.co.uk
© 200
9



Lifecycle of a Bundle
Spring Dynamic Modules
-
OSGi with Spring Framework
6
INSTALL
STARTING
ACTIVE
STOPPING
INSTALLED
RESOLVED
UNINSTALLED
STOP
UNINSTALL
UPDATE
REFRESH
UNINSTALL
RESOLVE
UPDATE
REFRESH
© 200
9



Service Registry
Spring Dynamic Modules
-
OSGi with Spring Framework
7
SERVICE REGISTRY
SERVICE
CONSUMER
SERVICE
PROVIDER
BIND
SERVICE
DESCRIPTION
© 200
9



OSGi

A Module System for Java

Clear boundaries

Dependencies

Metadata

Lifecycle

Service Registry
Spring Dynamic Modules
-
OSGi with Spring Framework
8
© 200
9



The
hard
-
coding
Way
Spring Dynamic Modules
-
OSGi with Spring Framework
9
© 200
9



The
hard
-
coding
Way

Register a Service

Register
ServiceRegistration reg = bundleContext.registerService(
ChatterBoxService.class.getName(),
twitterChatterbox, properties);

Unregister
reg.unregister();
Spring Dynamic Modules
-
OSGi with Spring Framework
10
© 200
9



The hard
-
coding Way

Consume a Service

Get Service
ServiceReference ref = bundleContext.getServiceReference(
ChatterBoxService.class.getName());
ChatterBoxService chatterbox =
(ChatterBoxService)bundleContext.getService(ref);

Unget Service
bundleContext.ungetService(ref);
Spring Dynamic Modules
-
OSGi with Spring Framework
11
© 200
9



The hard
-
coding Way

Consume a Service

Get Service with ServiceTracker
ServiceTracker tracker = new ServiceTracker(
bundleContext, LogService.class.getName(),
serviceTrackerCustomizer);
tracker.open();
LogService logService = (LogService) tracker.getService();
Spring Dynamic Modules
-
OSGi with Spring Framework
12
© 200
9



Declarative Services
Spring Dynamic Modules
-
OSGi with Spring Framework
13
© 200
9



Declarative Services

Declarative Services (DS) are part of OSGi R4 Specification

Service Compendium

Declaration of components in XML

OSGI
-
INF/<component>.xml

Components provides and depend on other Services

Components need a special bundle manifest header
e.g.
Service
-
Component: OSGI
-
INF/TwitterChatterBox.xml
Spring Dynamic Modules
-
OSGi with Spring Framework
14
© 200
9



Declarative Services
<?xml version="1.0" encoding="UTF
-
8"?>
<component name="com.trivadis.chatterbox.twitter">
<implementation
class="com.trivadis.chatterbox.twitter.service.internal.TwitterChatte
rboxServiceImpl"/>
<
service>
<provide
interface="com.trivadis.chatterbox.service.ChatterBoxService"/>
</service>
<reference name="LOGGER"
interface="
org.osgi.service.log.LogService
"
cardinality="0..n"
policy="dynamic"
bind="
addLogService
"
unbind="
removeLogService
"/>
</component>
Spring Dynamic Modules
-
OSGi with Spring Framework
15
© 200
9



Spring Dynamic Modules
Spring Dynamic Modules
-
OSGi with Spring Framework
16
© 200
9



Spring Dynamic Modules

Integration of Springs Dependency Injection and OSGi

Formerly known as Spring OSGi

XML files are located in META
-
INF/spring

Very similar approach compared to DS

Uses Spring DI for references to other Services and POJOs

(Almost) no dependencies on OSGi APIs

Components need a special bundle manifest header
e.g.
Spring
-
Context: META
-
INF/spring/bundle
-
context.xml, …
Spring Dynamic Modules
-
OSGi with Spring Framework
17
© 200
9



The Spring DM Idea
Spring Dynamic Modules
-
OSGi with Spring Framework
18
APPLICATION
CONTEXT
Imported Service
Exported Service
Spring Bean
APPLICATION
CONTEXT
APPLICATION
CONTEXT
SPRING & SPRING DM
OSGI FRAMEWORK
JVM
© 200
9



Spring Dynamic Modules

bundle
-
context.xml
<beans … >
<bean id="twitterChatterBox"
class="com.trivadis.chatterbox.twitter.service.internal.TwitterChatt
erboxServiceImpl">
<property name="logService" ref="
logServiceOsgi
" />
</bean>
</beans>
Spring Dynamic Modules
-
OSGi with Spring Framework
19
© 200
9



Spring Dynamic Modules

bundle
-
context
-
osgi.xml
<beans:beans …>
<service ref="
twitterChatterBox
"
interface="com.trivadis.chatterbox.service.ChatterBoxService"/>
<reference id="
logServiceOsgi
"
interface="org.osgi.service.log.LogService" />
</beans:beans>
Spring Dynamic Modules
-
OSGi with Spring Framework
20
© 200
9



Spring Dynamic Modules

Also supports listeners, filters, and collections

Dynamics are handled by the framework

Proxies for service instances and collections

Method calls are buffered

Configurable timeouts

Annotation
-
Based Injection with
@ServiceReference
Spring Dynamic Modules
-
OSGi with Spring Framework
21
© 200
9



Blueprint Services
Spring Dynamic Modules
-
OSGi with Spring Framework
22
© 200
9



Blueprint Services

Blueprint Services are a Standard since OSGi R 4.2
and based on the Ideas of Spring DM 1.0

Spring DM 2.0 is the Reference Implementation (RI)

Apache Aries Blueprint is an other implementation

Extremely similar to Spring DM but a standard

XML files are located in META
-
INF/blueprint

Components need a special bundle manifest header
e.g. Bundle
-
Blueprint: OSGI
-
INF/blueprint/config.xml, …
Spring Dynamic Modules
-
OSGi with Spring Framework
23
© 200
9



Blueprint Services

config.xml
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="twitterChatterBox"
class="com.trivadis.chatterbox.twitter.service.internal.TwitterChatt
erboxServiceImpl">
<property name="logService" ref="logService" />
</bean>
<service ref="twitterChatterBox"
interface="com.trivadis.chatterbox.service.ChatterBoxService" />
<reference id="logService"
interface="org.osgi.service.log.LogService" />
</blueprint>
Spring Dynamic Modules
-
OSGi with Spring Framework
24
© 200
9



Comparison Spring DM vs. Blueprint Services
Dependency Injection
Spring Dynamic Modules
-
OSGi with Spring Framework
25

Constructor Injection

Setter Injection

Field Injection

Method Injection

Arbitrary Method Injection

Autowiring
Spring DM

YES

YES

YES

YES

YES

YES
Blueprint Services

YES

YES

NO

NO

NO

NO
Source: Spring Dynamic Modules Reference Guide 2.0.0.M1
© 200
9



Comparison Spring DM vs. Blueprint Services
Component Lifecycle
Spring Dynamic Modules
-
OSGi with Spring Framework
26

Lazy Initialization

Bean Scopes

Custom Bean Scopes

Built
-
in Callbacks

Custom Callbacks

Initialization Processing
Spring DM

YES

YES

YES

YES

YES

YES
Blueprint Services

YES

YES

NO

NO

YES

NO
Source: Spring Dynamic Modules Reference Guide 2.0.0.M1
© 200
9



Demo
Spring Dynamic Modules
-
OSGi with Spring Framework
27
© 200
9



Conclusion

Should I use Declarative Services, Spring DM or Blueprint
Services?

DS, DM or Blueprint Services are better than program the life
cycles and service infrastructure by hand

If you already use Spring, use Spring DM

If you want to use Standards use DS or Blueprint

To switch from Blueprint Services to Spring DM you need just a
few changes in the XML configuration.
Spring Dynamic Modules
-
OSGi with Spring Framework
28
?
www.trivadis.com
Baden Basel Bern Brugg Lausanne Zürich Düsseldorf Frankfurt/M.
Freiburg i. Br. Hamburg München Stuttgart Wien



Thank you!