Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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!

...

Code Block
languagexml
titleConfiguring 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 Block
languagexml
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 Block
languagexml
titleUsing 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 Block
languagexml
titleUsing 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> 

...

Stripes uses the SpringInterceptor 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 Block
languagejava
titleInjecting 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; 
} 

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 Block
languagejava
titleAuto-wiring of Spring beans in an ActionBean
 
/** Setter to allow the BugManager to be injected. */ 
@SpringBean 
protected 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 - if the method name starts with 'set' then it is removed and the next character down-cased (following standard JavaBean rules), otherwise the entire method name is taken verbatim. 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.

...

Stripes can inject Spring beans through both method access and field access. The access type is determined by the placement of the @SpringBean annotation. If the annotation is directly on the field, e.g.:

Code Block
languagejava
 @SpringBean private BugManager bugManager;

then Stripes will use field access. If the annotation is on a method, e.g.:

Code Block
languagejava
 @SpringBean protected void setBugManager(BugManager bm) { ... }

...