Some time ago I wrote about how to use backing beans in mixed dependency injection environments. The blog entry describes how to write POJOs in JSF and Spring and use these in a JSF-based presentation. But, the configuration of the Web container is still missing.
Configuration
The configuration is very simple. We need a special listener, RequestContextListener, and a resolver, DelegatingVariableResolver, both from the Spring distribution, to do some “dispatching” when a backing bean identifier has to be mapped to a bean. This configuration allows to move all JSF-specific definitions to the Spring context.
To get a better overview of the managed backing beans we can divide the application context file into different ones. It’s a good idea to follow the packages the beans reside in. To recognize the context I also prefix the files with a “spring-” to prevent subfolders in the /WEB-INF.
Here’s the code for the web.xml (having some beans in the packages “security”, “commons” and “context”):
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/spring-security.xml,
/WEB-INF/spring-commons.xml,
/WEB-INF/spring-context.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Here’s the entry for the faces-config.xml:
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
Maven Build
If you use Maven2 you have to add some dependencies for Spring. Dependent from other frameworks you use, e.g. Acegi, you may have to add more dependencies. Maybe with scope “provided”. Have a look at the target folder to check for unnecessary jars.
The dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm-attrs</artifactId>
<version>1.5.3</version>
</dependency>

I'm Rainer Eschen, an IT-Business Architect, who looks back on more than 20 years of programming experience. Since 1994 I'm an IT professional with a focus on consulting and architecture. I have also been part of "the source" for three years, working as Sun Sales Support Engineer and Sun Java Center Architect at Sun Microsystems, Germany.