Browse Source

Fix resource-chain XML syntax for cache

This change moves the resource-cache configuration to the
<resource-chain/> tag, since enabling/disabling resource cache should
be driven by a property or a SpEL expression.

So now that configuration can be set with XML attributes:

  <mvc:resource-chain resource-cache="true"
  cache-manager="resourceCache" cache-name="test-resource-cache">

In order to mirror the JavaConfig behavior, the "resource-cache"
attribute is required.

Issue: SPR-12129
pull/598/head
Brian Clozel 12 years ago
parent
commit
7b93cefe64
  1. 8
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/ResourcesBeanDefinitionParser.java
  2. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java
  3. 22
      spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd
  4. 2
      spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-resources-chain-no-auto.xml
  5. 3
      spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-resources-chain.xml

8
spring-webmvc/src/main/java/org/springframework/web/servlet/config/ResourcesBeanDefinitionParser.java

@ -200,8 +200,8 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser { @@ -200,8 +200,8 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
private void parseResourceCache(ManagedList<? super Object> resourceResolvers,
ManagedList<? super Object> resourceTransformers, Element element, Object source) {
Element resourceCacheElement = DomUtils.getChildElementByTagName(element, "resource-cache");
if (resourceCacheElement != null) {
String resourceCache = element.getAttribute("resource-cache");
if ("true".equals(resourceCache)) {
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
@ -214,8 +214,8 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser { @@ -214,8 +214,8 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
cachingTransformerDef.setConstructorArgumentValues(cavs);
String cacheManagerName = resourceCacheElement.getAttribute("cache-manager");
String cacheName = resourceCacheElement.getAttribute("cache-name");
String cacheManagerName = element.getAttribute("cache-manager");
String cacheName = element.getAttribute("cache-name");
if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
cavs.addIndexedArgumentValue(0, cacheManagerRef);

2
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java

@ -99,7 +99,7 @@ public class ResourceHandlerRegistration { @@ -99,7 +99,7 @@ public class ResourceHandlerRegistration {
*
* @param cacheResources whether to cache the result of resource resolution;
* setting this to "true" is recommended for production (and "false" for
* development, especially when applying a version strategy.
* development, especially when applying a version strategy).
* @return the same {@link ResourceHandlerRegistration} instance for chained method invocation
* @since 4.1
*/

22
spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd

@ -338,18 +338,6 @@ @@ -338,18 +338,6 @@
</xsd:complexType>
</xsd:element>
<xsd:complexType name="resource-cache">
<xsd:annotation>
<xsd:documentation source="org.springframework.web.servlet.resource.CachingResourceResolver"><![CDATA[
A ResourceResolver that resolves resources from a Cache or otherwise delegates to the resolver chain
and saves the result in the cache. Can use a custom Cache if a CacheManager is provided as a bean reference
in the "cache-manager" attribute, and the cache name provided in the "cache-name" attribute.
]]></xsd:documentation>
</xsd:annotation>
<xsd:attribute name="cache-manager" type="xsd:string" use="optional"/>
<xsd:attribute name="cache-name" type="xsd:string" use="optional"/>
</xsd:complexType>
<xsd:complexType name="content-version-strategy">
<xsd:annotation>
<xsd:documentation source="org.springframework.web.servlet.resource.ContentVersionStrategy"><![CDATA[
@ -471,16 +459,22 @@ @@ -471,16 +459,22 @@
<xsd:annotation>
<xsd:documentation source="org.springframework.web.servlet.config.annotation.ResourceChainRegistration"><![CDATA[
Assists with the registration of resource resolvers and transformers.
Unless set to "false", the auto-registration add default Resolvers (a PathResourceResolver) and Transformers
Unless set to "false", the auto-registration adds default Resolvers (a PathResourceResolver) and Transformers
(CssLinkResourceTransformer, if a VersionResourceResolver has been manually registered).
The resource-cache attribute sets whether to cache the result of resource resolution/transformation;
setting this to "true" is recommended for production (and "false" for development).
A custom Cache can be configured if a CacheManager is provided as a bean reference
in the "cache-manager" attribute, and the cache name provided in the "cache-name" attribute.
]]></xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="resource-cache" type="resource-cache" minOccurs="0" maxOccurs="1"/>
<xsd:element name="resolvers" type="resource-resolvers" minOccurs="0" maxOccurs="1"/>
<xsd:element name="transformers" type="resource-transformers" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="resource-cache" type="xsd:boolean" use="required"/>
<xsd:attribute name="auto-registration" type="xsd:boolean" default="true" use="optional"/>
<xsd:attribute name="cache-manager" type="xsd:string" use="optional"/>
<xsd:attribute name="cache-name" type="xsd:string" use="optional"/>
</xsd:complexType>
<xsd:element name="resources">

2
spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-resources-chain-no-auto.xml

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/">
<mvc:resource-chain auto-registration="false">
<mvc:resource-chain resource-cache="false" auto-registration="false">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:fixed-version-strategy version="abc" patterns="/**/*.js"/>

3
spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-resources-chain.xml

@ -22,8 +22,7 @@ @@ -22,8 +22,7 @@
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/">
<mvc:resource-chain>
<mvc:resource-cache cache-manager="resourceCache" cache-name="test-resource-cache"/>
<mvc:resource-chain resource-cache="true" cache-manager="resourceCache" cache-name="test-resource-cache">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:fixed-version-strategy version="abc" patterns="/**/*.js"/>

Loading…
Cancel
Save