Spring with Stripes

Skip to end of metadata
Go to start of metadata

Stripes is a web application framework that was designed to be easy to use and increase developer productivity. Spring is, primarily, a lightweight component container (although nowadays it is many other things too) designed to be easy to use and increase developer productivity. Naturally you might think to use the two together.

Stripes integrates with Spring to provide your ActionBean classes access to Spring resources in the form of configured Spring beans. It does this using a simple, behind the scenes, injecting of Spring beans into ActionBeans. You'll need to perform a little configuration to get things started, but once that's done you can use your Spring beans in your Stripes web application without writing another line of XML!

Installing and Configuring Spring

This section provides a basic overview of how to install and configure Spring so that you can use it with your Stripes application. It will not cover all the possible ways of doing this, but simply one that works. Start off by downloading the latest version of Spring from http://www.springframework.org/download. Expand the downloaded file and find the file dist/spring.jar. Copy this file into your application's classpath, probably under WEB-INF/lib.

Slimming down your classpath
If having lots of classes in your classpath that you may not be using bothers you, you may want to take the time to go through the various spring-* jars in the dist directory and assemble the minimal set of classes you need to use Spring. Me, I'm too lazy to do that so I just use spring.jar which includes everything I need and a bunch more too.

Once you have the jar or jars in place you'll need to create a spring context file, and configure a web application context in your web.xml file. Let's start by taking a look at the web.xml:

Configuring Spring in the web.xml
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>

The listener stanza simply configures a Spring listener class, which is used to bootstrap Spring in the web application environment. The second stanza provides Spring with the context parameter it uses to locate the context file being used. In this case we are using /WEB-INF/spring-context.xml. Any name can be used, but it is generally a good idea to put it in WEB-INF or a subdirectory thereof, as this will ensure it is not served up to clients of your web application!

The second thing we need to do is configure the Spring context. For more details on this you are better off reading the Spring documentation at http://www.springframework.org/docs/reference/index.html. Below is a sample context that we might use to access the various manager components in the Bugzooky application as Spring beans.

/WEB-INF/spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean name="/bugzooky/BugManager" 
          class="net.sourceforge.stripes.examples.bugzooky.biz.BugManager"/>
    <bean name="/bugzooky/PersonManager"
          class="net.sourceforge.stripes.examples.bugzooky.biz.PersonManager"/>
    <bean name="/bugzooky/ComponentManager"
          class="net.sourceforge.stripes.examples.bugzooky.biz.ComponentManager"/>
</beans>

Configuring Stripes for Spring

Now that we have Spring all set up we need to do a couple of things to make Stripes a bit more Spring aware. In your web.xml file locate the initialization parameters for the Stripes Filter. If they do not contain the following parameter then simply copy/paste it in (without any changes):

Making Stripes use a Runtime Configuration
<init-param>
    <param-name>Configuration.Class</param-name>
    <param-value>net.sourceforge.stripes.config.RuntimeConfiguration</param-value>
</init-param>

The above line tell Stripes to use a RuntimeConfiguration to drive the configuration. The RuntimeConfiguration allows you to replace components in Stripes, which is handy because that's exactly what we need to do next. The following initialization parameter, when added to the Stripes Filter, will cause Stripes to use a different ActionResolver, that is Spring aware.

Using the Spring aware ActionResolver with Stripes
<init-param>
    <param-name>ActionResolver.Class</param-name>
    <param-value>net.sourceforge.stripes.integration.spring.SpringAwareActionResolver</param-value>
</init-param>

Now we're done installing things and monkeying with configurations. Now let's do some coding!

Accessing Spring Beans in your ActionBean

Stripes uses the SpringAwareActionResolver to inject Spring beans into ActionBeans right after the ActionBean is instantiated. To do this, it must be told how to inject beans and what to inject. As opposed to pushing the linkage out to an XML file, a simple annotation is used. The standard case would look like this:

Injecting a Spring bean into an ActionBean
/** Setter to allow the BugManager to be injected. */
@SpringBean("/bugzooky/BugManager")
public void setBugManager(BugManager bm) {
    this.bm  = bm;
}

This is the safest way to inject Spring beans into your ActionBean, and is strongly recommeded. There are two other options available to you, that we'll call auto-wire by name and auto-wire by type. If you omit the value of the @SpringBean annotation thusly:

Auto-wiring of Spring beans in an ActionBean
/** Setter to allow the BugManager to be injected. */
@SpringBean
public void setBugManager(BugManager bm) {
    this.bm  = bm;
}

then Stripes will first attempt to auto-wire by name, and then by type. First the name of the desired bean is derived from the method name using standard JavaBean rules; in the example above this yields bugManager. The Spring context is then queried to see if it contains a bean called bugManager. This query is case insensitive and path insensitive, resulting in a match to the /bugzooky/BugManager bean in the example above.

If the context does not contain a bean with a matching name then auto-wiring by type is a last ditch effort. In this case if a single bean exists in the context that is of the type required by the annotated method (BugManager in this case) then that bean will be selected and injected into the ActionBean.

It's worth noting that although ActionBeans themselves are not Spring beans using this approach, the beans injected into Spring beans are completely managed by Spring and have access to all the Spring services any other Spring bean would.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.