org.springframework.web.context.support
Class ServletContextPropertyPlaceholderConfigurer

java.lang.Object
  extended byorg.springframework.beans.factory.config.PropertyResourceConfigurer
      extended byorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer
          extended byorg.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer
All Implemented Interfaces:
BeanFactoryAware, BeanFactoryPostProcessor, BeanNameAware, Ordered, ServletContextAware

public class ServletContextPropertyPlaceholderConfigurer
extends PropertyPlaceholderConfigurer
implements ServletContextAware

Subclass of PropertyPlaceholderConfigurer that resolves placeholders as ServletContext init parameters (that is, web.xml context-param entries).

Can be combined with "locations" and/or "properties" values in addition to web.xml context-params. Alternatively, can be defined without local properties, to resolve all placeholders as web.xml context-params (or JVM system properties).

If a placeholder could not be resolved against the provided local properties within the application, this configurer will fall back to ServletContext parameters. Can also be configured to let ServletContext init parameters override local properties (contextOverride=true).

Optionally supports searching of ServletContext attributes: If turned on, an otherwise unresolvable placeholder will matched against the corresponding ServletContext attribute, using its stringified value if found. This can be used to feed dynamic values into Spring's placeholder resolution.

If not running within a WebApplicationContext (or any other context that is able to satisfy the ServletContextAware callback), this class will behave like the default PropertyPlaceholderConfigurer. This allows for keeping ServletContextPropertyPlaceholderConfigurer definitions in test suites.

Since:
1.1.4
Author:
Juergen Hoeller
See Also:
PropertyResourceConfigurer.setLocations(org.springframework.core.io.Resource[]), PropertyResourceConfigurer.setProperties(java.util.Properties), PropertyPlaceholderConfigurer.setSystemPropertiesModeName(java.lang.String), setContextOverride(boolean), setSearchContextAttributes(boolean)

Field Summary
 
Fields inherited from class org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, SYSTEM_PROPERTIES_MODE_FALLBACK, SYSTEM_PROPERTIES_MODE_NEVER, SYSTEM_PROPERTIES_MODE_OVERRIDE
 
Fields inherited from class org.springframework.beans.factory.config.PropertyResourceConfigurer
logger
 
Constructor Summary
ServletContextPropertyPlaceholderConfigurer()
           
 
Method Summary
protected  String resolvePlaceholder(String placeholder, Properties props)
          Resolve the given placeholder using the given properties.
protected  String resolvePlaceholder(String placeholder, ServletContext servletContext, boolean searchContextAttributes)
          Resolves the given placeholder using the init parameters and optionally also the attributes of the given ServletContext.
 void setContextOverride(boolean contextOverride)
          Set whether ServletContext init parameters (and optionally also ServletContext attributes) should override local properties within the application.
 void setSearchContextAttributes(boolean searchContextAttributes)
          Set whether to search for matching a ServletContext attribute before checking a ServletContext init parameter.
 void setServletContext(ServletContext servletContext)
          Set the ServletContext that this object runs in.
 
Methods inherited from class org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
parseBeanDefinition, parseGenericArgumentValues, parseIndexedArgumentValues, parseList, parseMap, parsePropertyValues, parseSet, parseString, parseString, parseValue, processProperties, resolvePlaceholder, setBeanFactory, setBeanName, setIgnoreUnresolvablePlaceholders, setPlaceholderPrefix, setPlaceholderSuffix, setSystemPropertiesMode, setSystemPropertiesModeName
 
Methods inherited from class org.springframework.beans.factory.config.PropertyResourceConfigurer
convertProperties, convertPropertyValue, getOrder, postProcessBeanFactory, setFileEncoding, setIgnoreResourceNotFound, setLocation, setLocations, setOrder, setProperties, setPropertiesPersister
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletContextPropertyPlaceholderConfigurer

public ServletContextPropertyPlaceholderConfigurer()
Method Detail

setContextOverride

public void setContextOverride(boolean contextOverride)
Set whether ServletContext init parameters (and optionally also ServletContext attributes) should override local properties within the application. Default is false: ServletContext settings serve as fallback.

Note that system properties will still override ServletContext settings, if the system properties mode is set to "SYSTEM_PROPERTIES_MODE_OVERRIDE".

See Also:
PropertyPlaceholderConfigurer.setSystemPropertiesModeName(java.lang.String), PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE

setSearchContextAttributes

public void setSearchContextAttributes(boolean searchContextAttributes)
Set whether to search for matching a ServletContext attribute before checking a ServletContext init parameter. Default is false.

If turned on, the configurer will look for a ServletContext attribute with the same name as the placeholder, and use its stringified value if found. Exposure of such ServletContext attributes can be used to dynamically override init parameters defined in web.xml, for example in a custom context listener.

See Also:
ServletContext.getAttribute(java.lang.String)

setServletContext

public void setServletContext(ServletContext servletContext)
Description copied from interface: ServletContextAware
Set the ServletContext that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked after ApplicationContextAware's setApplicationContext.

Specified by:
setServletContext in interface ServletContextAware
Parameters:
servletContext - ServletContext object to be used by this object

resolvePlaceholder

protected String resolvePlaceholder(String placeholder,
                                    Properties props)
Description copied from class: PropertyPlaceholderConfigurer
Resolve the given placeholder using the given properties. Default implementation simply checks for a corresponding property key.

Subclasses can override this for customized placeholder-to-key mappings or custom resolution strategies, possibly just using the given properties as fallback.

Note that system properties will still be checked before respectively after this method is invoked, according to the system properties mode.

Overrides:
resolvePlaceholder in class PropertyPlaceholderConfigurer
Parameters:
placeholder - the placeholder to resolve
props - the merged properties of this configurer
Returns:
the resolved value, of null if none
See Also:
PropertyPlaceholderConfigurer.setSystemPropertiesMode(int)

resolvePlaceholder

protected String resolvePlaceholder(String placeholder,
                                    ServletContext servletContext,
                                    boolean searchContextAttributes)
Resolves the given placeholder using the init parameters and optionally also the attributes of the given ServletContext.

Default implementation checks ServletContext attributes before init parameters. Can be overridden to customize this behavior, potentially also applying specific naming patterns for parameters and/or attributes (instead of using the exact placeholder name).

Parameters:
placeholder - the placeholder to resolve
servletContext - the ServletContext to check
searchContextAttributes - whether to search for a matching ServletContext attribute
Returns:
the resolved value, of null if none


Copyright (C) 2003-2004 The Spring Framework Project.