Skip to content

Commit 7b93cef

Browse files
committed
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
1 parent c9c3857 commit 7b93cef

File tree

5 files changed

+15
-22
lines changed

5 files changed

+15
-22
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ private void parseResourceChain(RootBeanDefinition resourceHandlerDef, ParserCon
200200
private void parseResourceCache(ManagedList<? super Object> resourceResolvers,
201201
ManagedList<? super Object> resourceTransformers, Element element, Object source) {
202202

203-
Element resourceCacheElement = DomUtils.getChildElementByTagName(element, "resource-cache");
204-
if (resourceCacheElement != null) {
203+
String resourceCache = element.getAttribute("resource-cache");
204+
if ("true".equals(resourceCache)) {
205205
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
206206

207207
RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
@@ -214,8 +214,8 @@ private void parseResourceCache(ManagedList<? super Object> resourceResolvers,
214214
cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
215215
cachingTransformerDef.setConstructorArgumentValues(cavs);
216216

217-
String cacheManagerName = resourceCacheElement.getAttribute("cache-manager");
218-
String cacheName = resourceCacheElement.getAttribute("cache-name");
217+
String cacheManagerName = element.getAttribute("cache-manager");
218+
String cacheName = element.getAttribute("cache-name");
219219
if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
220220
RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
221221
cavs.addIndexedArgumentValue(0, cacheManagerRef);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public ResourceHandlerRegistration setCachePeriod(Integer cachePeriod) {
9999
*
100100
* @param cacheResources whether to cache the result of resource resolution;
101101
* setting this to "true" is recommended for production (and "false" for
102-
* development, especially when applying a version strategy.
102+
* development, especially when applying a version strategy).
103103
* @return the same {@link ResourceHandlerRegistration} instance for chained method invocation
104104
* @since 4.1
105105
*/

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,6 @@
338338
</xsd:complexType>
339339
</xsd:element>
340340

341-
<xsd:complexType name="resource-cache">
342-
<xsd:annotation>
343-
<xsd:documentation source="org.springframework.web.servlet.resource.CachingResourceResolver"><![CDATA[
344-
A ResourceResolver that resolves resources from a Cache or otherwise delegates to the resolver chain
345-
and saves the result in the cache. Can use a custom Cache if a CacheManager is provided as a bean reference
346-
in the "cache-manager" attribute, and the cache name provided in the "cache-name" attribute.
347-
]]></xsd:documentation>
348-
</xsd:annotation>
349-
<xsd:attribute name="cache-manager" type="xsd:string" use="optional"/>
350-
<xsd:attribute name="cache-name" type="xsd:string" use="optional"/>
351-
</xsd:complexType>
352-
353341
<xsd:complexType name="content-version-strategy">
354342
<xsd:annotation>
355343
<xsd:documentation source="org.springframework.web.servlet.resource.ContentVersionStrategy"><![CDATA[
@@ -471,16 +459,22 @@
471459
<xsd:annotation>
472460
<xsd:documentation source="org.springframework.web.servlet.config.annotation.ResourceChainRegistration"><![CDATA[
473461
Assists with the registration of resource resolvers and transformers.
474-
Unless set to "false", the auto-registration add default Resolvers (a PathResourceResolver) and Transformers
462+
Unless set to "false", the auto-registration adds default Resolvers (a PathResourceResolver) and Transformers
475463
(CssLinkResourceTransformer, if a VersionResourceResolver has been manually registered).
464+
The resource-cache attribute sets whether to cache the result of resource resolution/transformation;
465+
setting this to "true" is recommended for production (and "false" for development).
466+
A custom Cache can be configured if a CacheManager is provided as a bean reference
467+
in the "cache-manager" attribute, and the cache name provided in the "cache-name" attribute.
476468
]]></xsd:documentation>
477469
</xsd:annotation>
478470
<xsd:sequence>
479-
<xsd:element name="resource-cache" type="resource-cache" minOccurs="0" maxOccurs="1"/>
480471
<xsd:element name="resolvers" type="resource-resolvers" minOccurs="0" maxOccurs="1"/>
481472
<xsd:element name="transformers" type="resource-transformers" minOccurs="0" maxOccurs="1"/>
482473
</xsd:sequence>
474+
<xsd:attribute name="resource-cache" type="xsd:boolean" use="required"/>
483475
<xsd:attribute name="auto-registration" type="xsd:boolean" default="true" use="optional"/>
476+
<xsd:attribute name="cache-manager" type="xsd:string" use="optional"/>
477+
<xsd:attribute name="cache-name" type="xsd:string" use="optional"/>
484478
</xsd:complexType>
485479

486480
<xsd:element name="resources">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
77

88
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/">
9-
<mvc:resource-chain auto-registration="false">
9+
<mvc:resource-chain resource-cache="false" auto-registration="false">
1010
<mvc:resolvers>
1111
<mvc:version-resolver>
1212
<mvc:fixed-version-strategy version="abc" patterns="/**/*.js"/>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
2323

2424
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/">
25-
<mvc:resource-chain>
26-
<mvc:resource-cache cache-manager="resourceCache" cache-name="test-resource-cache"/>
25+
<mvc:resource-chain resource-cache="true" cache-manager="resourceCache" cache-name="test-resource-cache">
2726
<mvc:resolvers>
2827
<mvc:version-resolver>
2928
<mvc:fixed-version-strategy version="abc" patterns="/**/*.js"/>

0 commit comments

Comments
 (0)