PDA

View Full Version : Spring DM and eclipse RCP: DI for views/editors


djo.mos
Apr 21st, 2008, 01:02 PM
Hi :)
I have been playing with OSGi recently, and was at the point of dropping it before I stumble into OSGi DS and even better, Spring DM :cool: This is a nice work of art that you crafted there guys !

Well, I'm managing to export and import services between bundles using Spring DM 1.1 M1 via XML.
But when I combine this with eclipse RCP, it gets a little bit too complicated for me: Sorry if this question was already raised (I'm sure it was), but I can't manage to find it anywhere.
How to inject Spring Beans into a view or an editor (in terms of eclipse terminology) ?
I've found an example where a factory is used.
Isn't there a more direct/simpler way of doing this ? Optimally, is it possible to use component scanning and DI via annotations ? as I'm already doing in a Java EE env (JSF + Spring 2.5), but here, It's Java SE and I'm a bit lost.
Otherwise, I can live with XML :cool:

Many thanks.

Costin Leau
Apr 21st, 2008, 01:16 PM
Hi,

thanks for the nice words.
Currently there are no plans for the near future to support Eclipse RCP. I'm definitely a newbie when it comes to RCP but from what I saw the configuration will still stay - even if Spring-DM comes into play, it will simply replace the eclipse RCP with its own.
Things might be improved with annotation injection but still, I don't see much reduction.
However I might be wrong so if you have any suggestion, please let us know. Also, considering looking at Spring-RCP/SpringDesktop which takes care of the RCP side.

Cheers,

djo.mos
Apr 21st, 2008, 01:40 PM
Thanks for the quick reply :)
Okey, let me please reformulate my question:
What is the best way to retrieve in an object "Consumer" living in a bundle "B1" a Spring Bean exported as an OSGi service ("Service") in another Bundle "B2" ?

I think that we should retrieve the application Context in the activator and expose it with a static getter.
Thus, "Consumer" will call Activator.getApplicationContext().getBean("Service").

Is this how it should be done ?

Cheers.

Costin Leau
Apr 21st, 2008, 01:47 PM
You would use osg:reference for importing and osgi:service for exporting the bean. You should not go through the application context interface - why do dependency lookup when you can have dependency injection.

See the reference documentation for more information:
http://static.springframework.org/osgi/docs/current/reference/html/service-registry.html


<osgi:service id="messageService" ref="msgService"
interface="com.xyz.MessageService"/>

<osgi:reference id="messageService"
interface="com.xyz.MessageService"/>

djo.mos
Apr 21st, 2008, 02:22 PM
Yes, absolutely, we totally agree on that.
Yet, at some point of your code, you need to extract a service (or Spring Bean) from the Registry (or Application Context).
In my cas, it is a view (a class that implement a special interface), and which is unfortunately not managed by Spring, i.e created directly by eclipse with a new statement.
And inside this class, I need to retrieve a Service from the OSGi Registry ...

Costin Leau
Apr 21st, 2008, 05:15 PM
I might be wrong but doesn't plugin.xml help with this case by configuring your view? The retrieval can be done through some sort of utility class - the injection however is not that easy to achieve since the bean is not managed by Spring and, I would assume, doesn't follow the JavaBeans conventions.
Sure, one could use Spring IoC container to apply injections but that all and I think this can be done even right now - configure Spring-DM to retrieve the services that you need and then use the returned bean factory to inject properties into your views. It will not be easy and will take about the same amount of code as w/o the injection.

I'll contact Christian Dupuis - he knows more about Eclipse then I do and he can probably show me the missing parts.

djo.mos
Apr 22nd, 2008, 03:57 AM
Many thanks Costin, I would be grateful :)
I'll continue to search, but it is somehow weird that this stuff is such poorly documented over the net (or am I such a bad googler ?) ... I can't manage to find a complete example with source code where Spring DM is used with eclipse RCP ...

scosenz
Apr 25th, 2008, 11:53 PM
The following talk was presented at EclipseCon 2008: Agile RCP - Presenter First using Spring Dynamic Modules (http://www.eclipsecon.org/2008/?page=sub/&id=78)

PDF: https://eclipsecon.greenmeetingsystems.com/attachments/download/420
Example Source: http://eclipsecon.greenmeetingsystems.com/attachments/download/422

Enjoy!
Steve

Costin Leau
Apr 26th, 2008, 04:46 AM
Thanks Steve - I've added the links to the Spring-DM site.
Cheers,

djo.mos
Apr 28th, 2008, 05:04 AM
My turn: Many Thanks Steve: I will have a look at those.

scosenz
Apr 28th, 2008, 08:53 AM
Also, there's another good example on Marin Lippert's blog:
http://martinlippert.blogspot.com/2008/03/dependency-injection-for-extensions.html

To find the source, just unzip
org.eclipse.springframework.utils_1.0.0.zip (http://www.martinlippert.org/download/springextensionfactory_1.0.0.zip)
and then unjar org.eclipse.springframework.util_1.0.0.jar

-Steve