Spring Extension

Spring-Icon

This page explains how to use the Open SUIT Spring extension in order to integrate the Spring framework into your Open SUIT applications.

Requirements

Please refer to the Getting Started requirements section to see the base Open SUIT requirements.

In addition to those, integrating Spring also requires:

  • the Open SUIT Spring extension available in the Open SUIT standard distribution (/lib/optional/spring/opensuit-spring.jar),
  • the Spring Framework (version 2.0 or higher), with at least:
    • spring-core.jar,
    • spring-context.jar,
    • spring-beans.jar,
    • spring-web.jar.

Integrating Spring

Basically the Spring extension supports two integration modes:

  1. Declare all of your Spring configuration using standard Spring features, and let Open SUIT retrieve it through the ServletContext,
  2. Have Open SUIT setup your Spring configuration for the whole application.

You're free to use whichever you prefer.

Following chapters present the two modes.

First integration mode: the "Spring" way

As explained previously, this mode is just using the standard Spring way of declaring your configuration, then let Open SUIT retrieve it through the ServletContext.

First integration mode: the "Spring" way
1) Instantiate the Spring context from your web.xml

Spring offers several ways of instantiating your application context in a web application (see Spring documentation).

For instance, using a J2EE listener. Here is a web.xml excerpt that uses this technique:

[...]
<!-- declare you xml files here -->
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                WEB-INF/applicationContext-ui.xml
                WEB-INF/applicationContext-dao.xml
        </param-value>
</context-param>
[...]
<!-- declare the Spring listener that will instantiate the context -->
<listener>
        <listener-class>
                org.springframework.web.context.ContextLoaderListener
        </listener-class>
</listener>
[...]
2) Enabling Spring integration in Open SUIT

Now all we have to do is to tell Open SUIT it can load the Spring context and use beans from there.

This should be done in the main Open SUIT xml file (that declares the <base:Application> tag):

<base:Application [...] 
  xmlns:base="xmlmap://org.ow2.opensuit.xml.base/v1.0"
  xmlns:spring="xmlmap://org.ow2.opensuit.xml.spring/v1.0">
  [...]
  <GlobalBeans>
    [...]
    <!-- non-Spring beans can be declared here -->
    [...]
    <spring:SpringBeans SearchIn="WAR" CreateContext="false">
      <ConfigFiles>
        <spring:XmlConfig File="/WEB-INF/applicationContext-ui.xml"/>
        <spring:XmlConfig File="/WEB-INF/applicationContext-doa.xml"/>
      </ConfigFiles>
    </spring:SpringBeans>
  </GlobalBeans>
  [...]
</base:Application>

Remarks:

  1. The Spring extension namespace is added to the top element:
    xmlns:spring="xmlmap://org.ow2.opensuit.xml.spring/v1.0"
  2. The CreateContext attribute is set to false. This tells Open SUIT not to instantiate the Spring context, but to retrieve it from the ServletContext (as it should have been setup by the Spring listener).
  3. The ConfigFiles tag is not required in this integration mode as Open SUIT doesn't have to instantiate the Spring context.
    Nevertheless it is used at build-time by the Eclipse plugin to compute validation (therefore it will be able to validate your expressions using Spring beans).
  4. The SearchIn attribute may have 2 values:
    • WAR: tells Open SUIT to look for Spring configuration files in your web app root directory,
    • CLASSPATH: tells Open SUIT to look for Spring configuration files in your classpath.

Second integration mode: the "Open SUIT" way

In this mode, you will let Open SUIT instantiate the Spring context for you.

Second integration mode: the "Open SUIT" way
1) Leave the web.xml unchanged

As Open SUIT will instantiate the Spring context, you don't need to setup your web.xml with any Spring stuff anymore.

2) Enabling Spring integration in Open SUIT

With this solution you will have almost the same Open SUIT declaration except that the CreateContext attribute is set to true.

<base:Application [...] 
  xmlns:base="xmlmap://org.ow2.opensuit.xml.base/v1.0"
  xmlns:spring="xmlmap://org.ow2.opensuit.xml.spring/v1.0">
  [...]
  <GlobalBeans>
    [...]
    <!-- non-Spring beans can be declared here -->
    [...]
    <spring:SpringBeans SearchIn="WAR" CreateContext="true">
      <ConfigFiles>
        <spring:XmlConfig File="/WEB-INF/applicationContext-ui.xml"/>
        <spring:XmlConfig File="/WEB-INF/applicationContext-doa.xml"/>
      </ConfigFiles>
    </spring:SpringBeans>
  </GlobalBeans>
  [...]
</base:Application>

Using the Open SUIT "page-context" scope from Spring

Spring-Idea

Now you've integrated Spring with Open SUIT, you'll probably want to have some of your UI beans attached to the Open SUIT "page context" scope.

More information about page context here.

Here is how to proceed in your Spring configuration files:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        [...]
        <!-- 1) import Open SUIT scope declaration -->
        <import resource="classpath:openSuitScope.xml" />
        [...]
        <!-- 2) use the Open SUIT "page-context" scope -->
        <bean name="myBean" class="com.my.app.ui.MyBean" scope="page-context"/>
        [...]
</beans>

Using Spring beans from Open SUIT

Now you've done all this, you're free to use Spring beans in your expressions:

[...]
<base:html.form.field.TextField>
        [...]
        <!-- expression that uses a Spring bean -->
        <Bind>${myBean.text}</Bind>
</base:html.form.field.TextField>
[...]