Wednesday, June 11, 2008

Spring Dynamic Modules and Hibernate

I'm currently working on a Sourceforge project.
On the server side I want to use Spring DM and Hibernate.
Eclipse RCP and Spring DM builds the client.

I spent the last few evenings trying and searching for a working Spring DM/Hibernate example. I had a lot of classloading issues.
Yesterday I got it to work, you can download the plug-in here.

It's a simple application handling a User object and using HSQL as database.

I added all the dependent jars in the /lib directory. You can create separated plug-ins for them if you want.
Here a list of all the jar files, because thats where I had my problems:
  • cglib-nodep-2.1_3.jar
  • commons-collections.jar
  • commons-dbcp.jar
  • commons-pool.jar
  • dom4j-1.6.1.jar
  • hibernate3.jar
  • hsqldb.jar
  • jta.jar
  • spring-core.jar
  • spring-jdbc.jar
  • spring-orm.jar
  • spring-tx.jar // why is the org.springframework.dao package in this jar?

You can access the service in the Test class:

package com.blogspot.swissdev.springservice;

import org.osgi.framework.BundleContext;

* @author Flavio Donze
public class Test {

private BundleContext context = Activator.getDefault().getContext();

public void start() {
System.out.println("starting the test...");

UserService service = (UserService) context.getService(context.getServiceReference(UserService.class.getName()));

User user = (User) context.getService(context.getServiceReference(User.class.getName()));

for (User u : service.findAll())
System.out.println("User: "+u.getId() + ", " + u.getUsername() + ", " + u.getPassword());

And here is my Spring configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="META-INF/spring/"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<property name="hibernateProperties">
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="">${}</prop>

<bean id="test" init-method="start" class="com.blogspot.swissdev.springservice.Test"/>

<!-- USER beans, POJO, DAO, Service -->
<bean id="user" class="com.blogspot.swissdev.springservice.UserImpl" scope="prototype">

<bean id="userDao" class="com.blogspot.swissdev.springservice.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>

<bean id="userService" class="com.blogspot.swissdev.springservice.UserServiceImpl">
<property name="userDao" ref="userDao"/>

Since I just wanted to get Spring DM and Hibernate to work, I didn't really test the rest, just in case you encounter some bugs.

To setup your workspace with Spring DM read the first part of my previous post.

For this example I used:
Eclipse 3.4 RC3
Spring Dynamic Modules for OSGi(tm) 1.0.2

Wednesday, June 4, 2008

Eclipse RCP Application using Spring DM

Last week I was searching the web for some example of Spring DM running on a Eclipse RCP application.
Didn't find one, so I thought I could write a post.

Eclipse 3.4 RC3
Spring Dynamic Modules for OSGi(tm) 1.0.2

Now create a new Plug-in-Project, using the settings below:

As a next step you have to import the Spring DM Plug-ins.
File->Import->Plug-in Development->Plug-ins and Fragments.
Point the plug-in location to the extracted "spring-osgi-1.0.2/dist" directory.

On the next page, add the following plug-ins:
  • org.springframework.bundle.osgi.core
  • org.springframework.bundle.osgi.extender
You just imported the Spring DM OSGi part, you also need the actual Spring framework.
Do the same as above, but instead of the "dist" directory select the "spring-osgi-1.0.2/lib" dir and select those plugins:
  • org.springframework.bundle.spring.aop
  • org.springframework.bundle.spring.beans
  • org.springframework.bundle.spring.context
  • org.springframework.bundle.spring.core
  • org.springframework.osgi.aopalliance.osgi
We have to manually create a apache commons logging plugin.
Spring DM has some classloading problems with the existing plugin contained in the eclipse platform.
New->Plug-in Development->Plug-in from existing JAR archives.
Add External...
Now if you have the Spring framework including dependencies on your machine select the commons-logging.jar located at "spring-framework-2.5.x/lib/jakarta-commons", otherwise download the jar here.
Name the plugin "org.apache.commons.logging

Ok, so now your workspace is ready.
Let's create a Spring service.

package swissdev.springdm;

public interface IMyService {

String getSomething();

package swissdev.springdm;

public class MyService implements IMyService {

public String getSomething() {
return "something";

public void start() {
System.out.print("start service");

public void stop() {
System.out.print("stop service");


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="myService"



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<osgi:service id="myServiceOsgi" ref="myService" interface="swissdev.springdm.IMyService"/>


Our application is basically ready to launch, so lets do that.
Right click on the swissdev.springdm plug-in: Run-As -> Eclipse Application.
A simple window containing a view is launched, nothing special.

Open the run configuration: Run -> Run Configurations...
Select your "swissdev.springdm.application" launch config and change to the "Arguments" tab.
Add "-console" to the Program arguments.
Switch to the Plug-ins tab and select all the "Workspace" plug-ins, hit the "Add Required Plug-ins" button and run again.
Type ss in the console, you will get the following output:

So our Spring plug-ins are not active.
Open the run configurations again and switch to the "Configurations" tab, change the Configurations File option to "Use existing config.ini file as a template" and enter ${workspace_loc}/swissdev.springdm/config.ini.
We can get the default generated config.ini in our workspace.
It's located at /.metadata/.plugins/org.eclipse.pde.core/swissdev.springdm.application/config.ini, copy it to your plug-in.
Right click on the ini file and select Open With -> Text Editor.
Ctrl+f Find: extender
You should find add @start at the end:
Do the same with the swissdev.springdm plugin.

Launch again, you will see a lot of output and somewhere between "start service", thats the output we defined in the MyService class.
Spring DM is working!

To use our service we first need to modify our Activator:

private BundleContext context;

public BundleContext getContext() {
return context;

public void start(BundleContext context) throws Exception {
plugin = this;
this.context = context;

Open the generated View class and edit the createPartControl method:

public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.addDoubleClickListener(new IDoubleClickListener() {

public void doubleClick(DoubleClickEvent event) {
ServiceTracker tracker = new ServiceTracker(Activator.getDefault().getContext(), IMyService.class.getName(),null);;
IMyService service = (IMyService) tracker.getService();

Launch again and double click one of the itms in the window, in the console there should appear an "something" output.

That was it....