
Stripes is a web application framework that was designed to be easy to use and increase developer productivity. [Spring|http://www.springframework.org/] 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!
h2. 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}}.
{tip:title=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.
{tip}
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}}:
{code:language=xml|title=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>
{code}
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.
{code:language=xml|title=/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>
{code}
h2. 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 you do not already have the {{Interceptor.Classes}} parameter defined, add the following:
{code:language=xml|title=Using the Spring Interceptor (Stripes 1.5 and up)}
<init-param>
<param-name>Interceptor.Classes</param-name>
<param-value>
net.sourceforge.stripes.integration.spring.SpringInterceptor
</param-value>
</init-param>
{code}
The parameter tells Stripes to use the Spring interceptor. This will work with Stripes 1.5 and up; if you are still using an older version, you also need to tell Stripes not to stop using the Before/After method interceptor (which is configured by default):
{code:language=xml|title=Using the Spring Interceptor (Stripes 1.4.x and earlier}
<init-param>
<param-name>Interceptor.Classes</param-name>
<param-value>
net.sourceforge.stripes.integration.spring.SpringInterceptor,
net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor
</param-value>
</init-param>
{code}
We're done installing things and monkeying with configurations. Now let's do some coding!
h2. Accessing Spring Beans in your ActionBean
Stripes uses the [SpringInterceptor|http://stripes.sourceforge.net/docs/current/javadoc/index.html?net/sourceforge/stripes/integration/spring/SpringInterceptor.html] to inject Spring beans into ActionBeans 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 something like this:
{code:title=Injecting a Spring bean into an ActionBean}
/** Setter to allow the BugManager to be injected. */
@SpringBean("/bugzooky/BugManager")
public void injectBugManager(BugManager bm) {
this.bm = bm;
}
{code}
Specifying the exact name and path of the Spring bean you want injected is probably the safest way to go. There are, however, two other options available to you; we'll call auto-wire by name and auto-wire by type. If you omit the value of the {{@SpringBean}} annotation thusly:
{code:title=Auto-wiring of Spring beans in an ActionBean}
/** Setter to allow the BugManager to be injected. */
@SpringBean
protected void setBugManager(BugManager bm) {
this.bm = bm;
}