Change resource handler XML Namespace
This commit changes the way a <mvc:resource-cache> can be configured with a user defined Cache instance. Now a reference to a CacheManager Bean and a Cache name must be provided. This is a more flexible configuration for typical XML setups. <mvc:resource-cache cache-manager="resourceCache" cache-name="test-resource-cache"/> Issue: SPR-12129
This commit is contained in:
parent
4df05d1f98
commit
76c46fdbe3
|
@ -181,10 +181,12 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||||
cachingTransformerDef.setConstructorArgumentValues(cavs);
|
cachingTransformerDef.setConstructorArgumentValues(cavs);
|
||||||
|
|
||||||
String cacheBeanName = resourceCacheElement.getAttribute("cache");
|
String cacheManagerName = resourceCacheElement.getAttribute("cache-manager");
|
||||||
if (StringUtils.hasText(cacheBeanName)) {
|
String cacheName = resourceCacheElement.getAttribute("cache-name");
|
||||||
RuntimeBeanReference cacheRef = new RuntimeBeanReference(cacheBeanName);
|
if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
|
||||||
cavs.addIndexedArgumentValue(0, cacheRef);
|
RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
|
||||||
|
cavs.addIndexedArgumentValue(0, cacheManagerRef);
|
||||||
|
cavs.addIndexedArgumentValue(1, cacheName);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
|
ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.web.servlet.resource;
|
package org.springframework.web.servlet.resource;
|
||||||
|
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
@ -40,6 +41,9 @@ public class CachingResourceResolver extends AbstractResourceResolver {
|
||||||
|
|
||||||
private final Cache cache;
|
private final Cache cache;
|
||||||
|
|
||||||
|
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
|
||||||
|
this(cacheManager.getCache(cacheName));
|
||||||
|
}
|
||||||
|
|
||||||
public CachingResourceResolver(Cache cache) {
|
public CachingResourceResolver(Cache cache) {
|
||||||
Assert.notNull(cache, "'cache' is required");
|
Assert.notNull(cache, "'cache' is required");
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.resource;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
@ -40,6 +41,9 @@ public class CachingResourceTransformer implements ResourceTransformer {
|
||||||
|
|
||||||
private final Cache cache;
|
private final Cache cache;
|
||||||
|
|
||||||
|
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
|
||||||
|
this(cacheManager.getCache(cacheName));
|
||||||
|
}
|
||||||
|
|
||||||
public CachingResourceTransformer(Cache cache) {
|
public CachingResourceTransformer(Cache cache) {
|
||||||
Assert.notNull(cache, "'cache' is required");
|
Assert.notNull(cache, "'cache' is required");
|
||||||
|
|
|
@ -342,10 +342,12 @@
|
||||||
<xsd:annotation>
|
<xsd:annotation>
|
||||||
<xsd:documentation source="org.springframework.web.servlet.resource.CachingResourceResolver"><![CDATA[
|
<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
|
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 provided as a bean reference in the "cache" attribute.
|
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:documentation>
|
||||||
</xsd:annotation>
|
</xsd:annotation>
|
||||||
<xsd:attribute name="cache" type="xsd:string" 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:complexType>
|
||||||
|
|
||||||
<xsd:complexType name="content-version-strategy">
|
<xsd:complexType name="content-version-strategy">
|
||||||
|
|
|
@ -32,6 +32,8 @@ import org.junit.Test;
|
||||||
import org.springframework.beans.DirectFieldAccessor;
|
import org.springframework.beans.DirectFieldAccessor;
|
||||||
import org.springframework.beans.TypeMismatchException;
|
import org.springframework.beans.TypeMismatchException;
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
|
import org.springframework.cache.Cache;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
@ -350,7 +352,8 @@ public class MvcNamespaceTests {
|
||||||
assertThat(resolvers.get(2), Matchers.instanceOf(PathResourceResolver.class));
|
assertThat(resolvers.get(2), Matchers.instanceOf(PathResourceResolver.class));
|
||||||
|
|
||||||
CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
|
CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
|
||||||
assertThat(cachingResolver.getCache(), Matchers.instanceOf(TestResourceCache.class));
|
assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
|
||||||
|
assertEquals("test-resource-cache", cachingResolver.getCache().getName());
|
||||||
|
|
||||||
VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(1);
|
VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(1);
|
||||||
assertThat(versionResolver.getStrategyMap().get("/**/*.js"),
|
assertThat(versionResolver.getStrategyMap().get("/**/*.js"),
|
||||||
|
@ -363,6 +366,10 @@ public class MvcNamespaceTests {
|
||||||
assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
|
assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
|
||||||
assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
|
assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
|
||||||
assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
|
assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
|
||||||
|
|
||||||
|
CachingResourceTransformer cachingTransformer = (CachingResourceTransformer) transformers.get(0);
|
||||||
|
assertThat(cachingTransformer.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
|
||||||
|
assertEquals("test-resource-cache", cachingTransformer.getCache().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -869,9 +876,15 @@ public class MvcNamespaceTests {
|
||||||
|
|
||||||
public static class TestPathHelper extends UrlPathHelper { }
|
public static class TestPathHelper extends UrlPathHelper { }
|
||||||
|
|
||||||
public static class TestResourceCache extends ConcurrentMapCache {
|
public static class TestCacheManager implements CacheManager {
|
||||||
public TestResourceCache(String name) {
|
@Override
|
||||||
super(name);
|
public Cache getCache(String name) {
|
||||||
|
return new ConcurrentMapCache(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> getCacheNames() {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/">
|
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/">
|
||||||
<mvc:resource-chain>
|
<mvc:resource-chain>
|
||||||
<mvc:resource-cache cache="resourceCache"/>
|
<mvc:resource-cache cache-manager="resourceCache" cache-name="test-resource-cache"/>
|
||||||
<mvc:resolvers>
|
<mvc:resolvers>
|
||||||
<mvc:version-resolver>
|
<mvc:version-resolver>
|
||||||
<mvc:fixed-version-strategy version="abc" patterns="/**/*.js"/>
|
<mvc:fixed-version-strategy version="abc" patterns="/**/*.js"/>
|
||||||
|
@ -36,7 +36,5 @@
|
||||||
</mvc:resource-chain>
|
</mvc:resource-chain>
|
||||||
</mvc:resources>
|
</mvc:resources>
|
||||||
|
|
||||||
<bean id="resourceCache" class="org.springframework.web.servlet.config.MvcNamespaceTests$TestResourceCache">
|
<bean id="resourceCache" class="org.springframework.web.servlet.config.MvcNamespaceTests$TestCacheManager"/>
|
||||||
<constructor-arg name="name" value="resourceCache"/>
|
|
||||||
</bean>
|
|
||||||
</beans>
|
</beans>
|
||||||
|
|
Loading…
Reference in New Issue