Blogs

Accessing Spring Beans from an OSGI Plugin

Will Ezell

In Spring you can create a bean, configure it and build declaratively by the Spring container.
In a dotCMS OSGI plugin, we provide a method to set and load a plugin specific context file from the plugin's Activator, but there is no built-in way to access the context once it's been loaded.

To obtain a reference to your custom created Spring Beans you would use the context like this:

context = new ClassPathXmlApplicationContext("/mycontext.xml");
MyBean mybean = (MyBean) context.getBean("MyBean");

In OSGI, each bundle has its own classpath, and when deployed to the OSGI container the code above will throw an exception because it can't find the file in the classpath.

To work around this issue we can create an ApplicationContextAware Bean, which we will call: SpringApplicationContext, and instantiate it in the Spring container by defining it in the spring configuration file that is loaded in the plugin's Activator. Because it is ApplicationContextAware, Spring will populate it with a reference to the ApplicationContext.

ds.setContextConfigLocation( "spring/example-servlet.xml" );

This is the bean definition we added to the example-servlet.xml file:

<bean id="springApplicationContext" class="com.dotmarketing.osgi.spring.SpringApplicationContext"></bean>

The class SpringApplicationContext implements: ApplicationContextAware and has a static variable CONTEXT that will hold a reference to the ApplicationContext.

public class SpringApplicationContext implements ApplicationContextAware {
  private static ApplicationContext CONTEXT;

This class provides a static method: getBean() that calls ApplicationContext.getBean() using it's static variable CONTEXT. This method will return the reference to the bean requested by name.

  public static Object getBean(String beanName) {
    return CONTEXT.getBean(beanName);
  }

Now all we have to do is create a bean, and add it to the spring/example-servlet.xml file.
In this example we created a bean named: HelloWorldBean with the following code:

public class HelloWorldBean {

  public String getHelloMessage(String userName) {
        // set the message
        String aMessage = (userName != null)
                ? "Hello EDIT " + userName + ", welcome to Spring  3.1 and dotCMS!  "
                : "Hello Total Stranger, welcome to Spring  3.1 and dotCMS!  ";
        return aMessage;
  }

  public String getHelloMessage() {
        // set the message
        String aMessage = "Hello World, Spring 3.1 and dotCMS!";
        return aMessage;
  }
}

We define the bean in the spring/example-servlet.xml file like this

  <bean id="helloWorldBean" class="com.dotmarketing.osgi.spring.HelloWorldBean"></bean>

To get a reference to our Bean to call its methods, we need to use the SpringApplicationContext class like this:

HelloWorldBean helloWorldBean = (HelloWorldBean) SpringApplicationContext.getBean("helloWorldBean");
String message = helloWorldBean.getHelloMessage();

In our example we call getHelloMessage() from our Spring Controller, allowing the Controller and any other classes included in your OSGI plugin to use your Spring Beans.

Will Ezell
Chief Technology Officer
February 22, 2013

Filed Under:

osgi plugin

Recommended Reading

Building a Business Case for Your Intranet: A Comprehensive Guide

Here’s a step-by-step guide to help you build a compelling business case for an intranet.

Reimagining dotCMS: Setting the Stage for Future Design

Explore the transformative journey of dotCMS as it unveils a new design system that harmonizes user interfaces and enhances cross-team collaboration.

What is a design token?

Explore how design tokens streamline consistency and adaptability across digital platforms, essential for modern design systems.

Highly Rated and Recommended

We're rated Excellent 4.2/5 stars on G2 - with 95+ verified reviews