Chapter 4. Component Usage
This chapter details how to use each of the components included in Snowdrop.
4.1. VFS-enabled Application Contexts
Note
From Spring 3.0 onward, the
ApplicationContext implementations shipped with the Spring framework are VFS-compatible. The components described in this section are included with Snowdrop to provide backwards compatibility, but are not necessarily required.
The
snowdrop-vfs.jar library supports resource scanning in the JBoss Virtual File System (VFS). It must be included in Spring-based applications that use classpath and resource scanning.
When the Spring framework performs resource scanning, it assumes that resources are either from a directory or a packaged JAR, and treats any URLs it encounters accordingly.
This assumption is not correct for the JBoss VFS, so Snowdrop provides a different underlying resource resolution mechanism by amending the functionality of the
PathMatchingResourcePatternResolver.
This is done by using one of two
ApplicationContext implementations provided by snowdrop-vfs.jar:
org.jboss.spring.vfs.context.VFSClassPathXmlApplicationContext- Replaces the Spring
org.springframework.context.support.ClassPathXmlApplicationContext. org.jboss.spring.vfs.context.VFSXmlWebApplicationContext- Replaces the Spring
org.springframework.web.context.support.XmlWebApplicationContext.
In many cases, the
VFSClassPathXmlApplicationContext is instantiated on its own, using:
ApplicationContext context = new VFSClassPathXmlApplicationContext("classpath:/context-definition-file.xml");
The
XmlWebApplicationContext is not instantiated directly. Instead, it is bootstrapped by either the ContextLoaderListener or the DispatcherServlet. Both classes have configuration options that allow users to replace the default application context type with a custom application context type.
To change the type of application context created by the
ContextLoaderListener, add the contextClass parameter as shown in the following example code:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-contexts/*.xml</param-value> </context-param> <context-param> <param-name>contextClass</param-name> <param-value> org.jboss.spring.vfs.context.VFSXmlWebApplicationContext </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
To change the type of application context created by the
DispatcherServlet, use the same contextClass parameter on the DispatcherServlet definition as shown:
<servlet> <servlet-name>spring-mvc-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-config.xml</param-value> </init-param> <init-param> <param-name>contextClass</param-name> <param-value> org.jboss.spring.vfs.context.VFSXmlWebApplicationContext </param-value> </init-param> </servlet>
Important
If you encounter the
ZipException when attempting to start the application, you need to replace the default ApplicationContext with one of the VFS-enabled implementations.
Caused by: java.util.zip.ZipException: error in opening zip file ... at org.springframework.core.io.support.PathMatchingResourcePatternResolver .doFindPathMatchingJarResources(PathMatchingResourcePatternResolver.java:448)