嗯,正如你所说,你想
cache
基础目标资源的全部内容,必须缓存
byte[]
从…起
inputStream
.
自从
<mvc:resources>
由支持
ResourceHttpRequestHandler
编写自己的子类并直接使用它而不是自定义标记是没有止境的。
并在超出范围内实现缓存逻辑
writeContent
方法:
public class CacheableResourceHttpRequestHandler extends ResourceHttpRequestHandler {
private Map<URL, byte[]> cache = new HashMap<URL, byte[]>();
@Override
protected void writeContent(HttpServletResponse response, Resource resource) throws IOException {
byte[] content = this.cache.get(resource.getURL());
if (content == null) {
content = StreamUtils.copyToByteArray(resource.getInputStream());
this.cache.put(resource.getURL(), content);
}
StreamUtils.copy(content, response.getOutputStream());
}
}
并将它从spring-config中用作泛型bean:
<bean id="staticResources" class="com.my.proj.web.CacheableResourceHttpRequestHandler">
<property name="locations" value="/public-resources/"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/resources/**=staticResources</value>
</property>
</bean>