How to invoke Spring beans in other osgi bundle

April 20 20092 Commented

Categorized Under: Java, Osgi, Software Engineer, Technology

Spring Dynamic Modules helps integrating Spring beans to Osgi platform seamlessly. It exports the Spring beans as Osgi services and it streats Spring-Osgi service as Spring bean. In the non-Osgi environment, developer can get the Spring beans via calling

ApplicationContext ctx = new ClasspathApplicationContext("example.xml");
BeanEx beanEx = (BeanEx)ctx.getBean("beanEx");

However, such calling will throw an exception in Spring DM platform because Spring requires the bundle context is existed. To overcome this issue, you just simply retrieving Spring-Osgi bean in the Activator class. Here are the example:

public class Activator implements BundleActivator {
 
    static ForumAuthenticationService forumService;
 
    static BundleContext bundleContext;
 
    public void start(BundleContext bundleContext) throws Exception {
        Activator.bundleContext = bundleContext;
    }
 
    public void stop(BundleContext arg0) throws Exception {
        forumService = null;
 
    }
 
    public static ForumAuthenticationService getAuthenticationService() {
        if (forumService == null) {
            OsgiBundleXmlApplicationContext context = new OsgiBundleXmlApplicationContext(
                    new String[] { "META-INF/spring/forum-context.xml" });
            context.setBundleContext(bundleContext);
            context.refresh();
            forumService = (ForumAuthenticationService) context
                    .getBean("forumAuthenticationService");
        }
 
        return forumService;
    }
 
}

In the above example, we need to get the spring service to make authenticate user in the legacy class and it is the simple solution how to retrieve the Spring-osgi service from some class in Osgi platform.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • blinkbits
  • BlinkList
  • blogmarks
  • BlogMemes
  • BlogMemes Cn
  • BlogMemes Fr
  • BlogMemes Jp
  • BlogMemes Sp
  • Blogosphere News
  • Blogsvine
  • blogtercimlap
  • Book.mark.hu
  • Bumpzee
  • co.mments
  • connotea
  • De.lirio.us
  • Design Float
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • feedmelinks
  • Fleck
  • Furl
  • GeenRedactie
  • Global Grind
  • Gwar
  • Haohao
  • HealthRanker
  • Hemidemi
  • Identi.ca
  • IndianPad
  • Internetmedia
  • kick.ie
  • Kirtsy
  • laaik.it
  • Leonaut
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Ma.gnolia
  • Meneame
  • MisterWong
  • MisterWong.DE
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • Ping.fm
  • PlugIM
  • Pownce
  • ppnow
  • Print
  • Propeller
  • Ratimarks
  • Rec6
  • Reddit
  • SalesMarks
  • Scoopeo
  • scuttle
  • Segnalo
  • Shadows
  • Simpy
  • Slashdot
  • Smarking
  • Socialogs
  • SphereIt
  • Spurl
  • StumbleUpon
  • Symbaloo
  • Taggly
  • TailRank
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • TwitThis
  • Upnews
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Buzz
  • YahooMyWeb
  • Yigg

Related posts:

  1. Spring Dynamic Modules (for Osgi) – the good complement for Osgi Spring Framework - the de factor standard IoC container...
  2. Using Spring Integration in Osgi platform How to use Spring Integration in Osgi platform. This post...
  3. Osgi – the first program Here is the simple Osgi application with Equinox framework and...
  4. Testing Spring beans against database with DbUnit This is another post of testing series we wrote so...
  5. Unit test with Spring Dynamic Modules Follow our story about developing services by using Spring Dynamic...

2 Responses to “How to invoke Spring beans in other osgi bundle”

  1. Costin Leau says:

    The activator should return as fast as possible since otherwise the entire OSGi platform waits for it. Moreover, why would you want to get a hold of the bean in the activator? Any work done with it, will slow down the OSGi platform even more.

    While the solution proposed above works, it has several nasty side effects. There are other alternatives such as:

    - wrapping the context creation in a separate thread so the activator returns fast
    - lookup the ApplicationContext (as Spring DM publishes as a service upon completion) and then use it – of course, also within a separate thread

    In all cases though you would have to pay attention to all possible outcomes, such as the context failing and thus not being published or Activator/OSGi platform shutdown.
    It’s considerably much more easier to leave the Spring DM extender to deal with these issues and simply make your own application IoC’ed rather then using dependency lookup.

    Hope this helps,

  2. admin says:

    Thanks Costin,

    The above solution is raised when we need to call spring bean service from a normal java class (a legacy application that it is not managed by Spring).

    The above code sample does a trick and it does not impact the time of initiating Activator because the static method getAuthenticationService is called after Activator start and Spring extender resolves dependencies (We control such flow in our calling function) – and we also handle the possible failures in calling method (in case we can not create the Spring context).

    One thing makes me feel uncomfortable is create the static variable bundleContext (it is a trick to set bundleContext later when create OsgiBundleXmlApplicationContext). Later our implementation, base on the fact that Spring DM registers Spring bean as Osgi service, we use the white board pattern to track whether some Osgi service (Spring bean) is resolved by Spring extender and we catch it.

Leave a Reply