The Spring Framework

org.springframework.web.servlet.handler.metadata
Class AbstractPathMapHandlerMapping

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.handler.AbstractHandlerMapping
              extended by org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
                  extended by org.springframework.web.servlet.handler.metadata.AbstractPathMapHandlerMapping
All Implemented Interfaces:
ApplicationContextAware, Ordered, ServletContextAware, HandlerMapping
Direct Known Subclasses:
CommonsPathMapHandlerMapping

public abstract class AbstractPathMapHandlerMapping
extends AbstractUrlHandlerMapping

Abstract implementation of the HandlerMapping interface that recognizes metadata attributes of type PathMap on application Controllers and automatically wires them into the current servlet's WebApplicationContext.

The path must be mapped to the relevant Spring DispatcherServlet in /WEB-INF/web.xml. It's possible to have multiple PathMap attributes on the one controller class.

Controllers instantiated by this class may have dependencies on middle tier objects, expressed via JavaBean properties or constructor arguments. These will be resolved automatically.

You will normally use this HandlerMapping with at most one DispatcherServlet in your web application. Otherwise you'll end with one instance of the mapped controller for each DispatcherServlet's context. You might want this -- for example, if one's using a .pdf mapping and a PDF view, and another a JSP view, or if using different middle tier objects, but should understand the implications. All Controllers with attributes will be picked up by each DispatcherServlet's context.

Author:
Rod Johnson, Juergen Hoeller

Field Summary
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Fields inherited from interface org.springframework.web.servlet.HandlerMapping
PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
AbstractPathMapHandlerMapping()
           
 
Method Summary
protected  void detectAndCreateHandlers(ConfigurableListableBeanFactory beanFactory)
          Look for all classes with a PathMap class attribute, instantiate them in the owning ApplicationContext, and register them as MVC handlers usable by the current DispatcherServlet.
protected abstract  Class[] getClassesWithPathMapAttributes()
          Use an attribute index to get a Collection of Class objects with the required PathMap attribute.
protected abstract  PathMap[] getPathMapAttributes(Class handlerClass)
          Use Attributes API to find PathMap attributes for the given handler class.
 void initApplicationContext()
          Calls the detectAndCreateHandlers method in addition to the superclass's initialization.
protected  void registerHandler(PathMap[] pathMaps, Object handler)
          Register the given handler for the URL paths indicated by the given PathMaps.
 void setAutowireMode(int autowireMode)
          Set the autowire mode for handlers.
 void setAutowireModeName(String constantName)
          Set the autowire mode for handlers, by the name of the corresponding constant in the AutowireCapableBeanFactory interface, e.g.
 void setDependencyCheck(boolean dependencyCheck)
          Set whether to perform a dependency check for objects on autowired handlers.
 
Methods inherited from class org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
exposePathWithinMapping, getHandlerInternal, getHandlerMap, getRootHandler, lookupHandler, registerHandler, registerHandler, setAlwaysUseFullPath, setLazyInitHandlers, setPathMatcher, setRootHandler, setUrlDecode, setUrlPathHelper
 
Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMapping
adaptInterceptor, extendInterceptors, getAdaptedInterceptors, getDefaultHandler, getHandler, getHandlerExecutionChain, getOrder, initInterceptors, setDefaultHandler, setInterceptors, setOrder
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractPathMapHandlerMapping

public AbstractPathMapHandlerMapping()
Method Detail

setAutowireModeName

public void setAutowireModeName(String constantName)
                         throws IllegalArgumentException
Set the autowire mode for handlers, by the name of the corresponding constant in the AutowireCapableBeanFactory interface, e.g. "AUTOWIRE_BY_NAME".

Parameters:
constantName - name of the constant
Throws:
IllegalArgumentException - if an invalid constant was specified
See Also:
setAutowireMode(int), AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT

setAutowireMode

public void setAutowireMode(int autowireMode)
Set the autowire mode for handlers. This determines whether any automagical detection and setting of bean references will happen.

Default is AUTOWIRE_AUTODETECT, which means either constructor autowiring or autowiring by type (depending on the constructors available in the class).

Parameters:
autowireMode - the autowire mode to set. Must be one of the constants defined in the AutowireCapableBeanFactory interface.
See Also:
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT

setDependencyCheck

public void setDependencyCheck(boolean dependencyCheck)
Set whether to perform a dependency check for objects on autowired handlers. Not applicable to autowiring a constructor, thus ignored there.

Default is "true".


initApplicationContext

public void initApplicationContext()
                            throws BeansException
Calls the detectAndCreateHandlers method in addition to the superclass's initialization.

Overrides:
initApplicationContext in class AbstractHandlerMapping
Throws:
ApplicationContextException - in case of initialization errors
BeansException - if thrown by ApplicationContext methods
See Also:
detectAndCreateHandlers(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

detectAndCreateHandlers

protected void detectAndCreateHandlers(ConfigurableListableBeanFactory beanFactory)
                                throws BeansException
Look for all classes with a PathMap class attribute, instantiate them in the owning ApplicationContext, and register them as MVC handlers usable by the current DispatcherServlet.

Parameters:
beanFactory - the ConfigurableListableBeanFactory to register the created handler instances with
Throws:
BeansException - if handler detection or creation failed
See Also:
PathMap, getClassesWithPathMapAttributes(), AutowireCapableBeanFactory.createBean(java.lang.Class, int, boolean), SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object)

registerHandler

protected void registerHandler(PathMap[] pathMaps,
                               Object handler)
                        throws BeansException,
                               IllegalStateException
Register the given handler for the URL paths indicated by the given PathMaps.

Parameters:
pathMaps - the PathMap attributes for the handler class
handler - the handler instance
Throws:
BeansException - if the handler couldn't be registered
IllegalStateException - if there is a conflicting handler registered

getClassesWithPathMapAttributes

protected abstract Class[] getClassesWithPathMapAttributes()
                                                    throws Exception
Use an attribute index to get a Collection of Class objects with the required PathMap attribute.

Returns:
a array of Class objects
Throws:
Exception

getPathMapAttributes

protected abstract PathMap[] getPathMapAttributes(Class handlerClass)
                                           throws Exception
Use Attributes API to find PathMap attributes for the given handler class. We know there's at least one, as the getClassNamesWithPathMapAttributes method return this class name.

Parameters:
handlerClass - the handler class to look for
Returns:
an array of PathMap objects
Throws:
Exception

The Spring Framework

Copyright © 2002-2007 The Spring Framework.