Using RingoJS with Spring

Skip to first unread message

James Cook

Aug 20, 2011, 9:55:55 PM8/20/11
Because there is more talk about using Spring and RingoJS together, I thought I would share some code we use to make it easy to get a reference to the Spring ApplicationContext from within your controllers. In a Java controller, you usually get a reference to Spring's context in your Servlet's init() method because you end up calling a static helper function and passing in your ServletConfig. RingoJS adheres to the JSGI specification which is agnostic to how infrastructure like Spring is made available to the controller layer.

RingoJS uses the org.ringojs.jsgi.JsgiServlet class to bootstrap the SSJS environment. We extend this class to provide some Spring helper functions that can be accessed from a RingoJS controller.

package org.ringojs.jsgi;

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class ExtJsgiServlet extends JsgiServlet {

    private ApplicationContext _springContext;

    public void init(ServletConfig config) throws ServletException {
        final ServletContext context = config.getServletContext();
        _springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    public ApplicationContext getSpringContext() {
        return _springContext;

    public Object getBean(String name) {
        return _springContext.getBean(name);

In order to launch your RIngoJS web application using this servlet, you simply replace the JsgiServlet with ExtJsgiServlet.




The cool thing is that the Servlet instance (the ExtJsgiServlet class) is now available to your controller using the request.env property. The 'env' property is part of the JSGI specification and it is the place where all of the custom keys used by the server or middleware MUST be placed. So, here is what your controller might look like:'/profiles/:profileId', function(req, profileId) {
    // Get access to the servlet using the 'env' garbage dump property
    var servlet = req.env.servlet;
    // Use the servlet's getBean(String) function to retrieve a configured bean from Spring
    var datasource = servlet.getBean('datasource');

    // Or maybe you are using Spring's reloadable resource bundles for externalizing your
    // website's localized content
    var context = servlet.getSpringContext();
    var welcomeMessage = context.getMessage('welcome', null, req.env.servletRequest.locale);
You may notice that the welcomeMessage example uses another property stored on the context; the servlet request. It's funny how the JSGI specification does not mention Locale. It is so important to globalization. At least RingoJS has made it accessible by exposing the original Java servlet request object off the env property..

Robert Thurnher

Aug 21, 2011, 8:12:11 AM8/21/11
Interesting, thanks for sharing!

I've put this on a wiki page to increase findability for (potential) users.

It's linked from "Java integration" page which is linked from "TODO"
page (which is linked from the wiki's main page), BTW.

IMHO, it would be great if more such info would be added there by you
guys actually using Ringo in according Java (enterprise) environments.

-- Robi


Aug 21, 2011, 2:15:54 PM8/21/11
to RingoJS
Like servlet can we implement filters & listeners too????

My project is using heavy dose of ringo + spring + E4x integration.It
would be really help full if people provide more of such code snippets
and configurations(as above).

> *web.xml*
> *
> *

James Cook

Aug 21, 2011, 4:29:33 PM8/21/11
Thanks robi. 

Indranil, if you are using Ringo's JsgiServlet to bootstrap your SSJS environment, then you can use Servlet listeners and filters.

To this end, we use Spring Security to secure all of our RingoJS access.

-- jim
Reply all
Reply to author
0 new messages