使用java配置使用SpringMVC3.2缓存图像

使用java配置使用SpringMVC3.2缓存图像,spring,caching,spring-mvc,Spring,Caching,Spring Mvc,我已经尝试过像其他stackoverflow问题中建议的那样,添加一个拦截器来缓存图像,但它似乎不起作用 @Override public void addInterceptors(InterceptorRegistry registry) { WebContentInterceptor wci = new WebContentInterceptor(); wci.setCacheSeconds(50000); Properties cacheMappings = ne

我已经尝试过像其他stackoverflow问题中建议的那样,添加一个拦截器来缓存图像,但它似乎不起作用

@Override
public void addInterceptors(InterceptorRegistry registry) {

    WebContentInterceptor wci = new WebContentInterceptor();
    wci.setCacheSeconds(50000);
    Properties cacheMappings = new Properties();
    cacheMappings.put("/avatars/**", "50000");
    wci.setCacheMappings(cacheMappings );

    registry.addInterceptor(wci);
    registry.addInterceptor(new DeviceResolverHandlerInterceptor());
}
我追踪到WebContent interceptor拦截器代码,它似乎在工作——匹配缓存映射并将缓存设置为50000秒。奇怪的是,它似乎第二次进入WebContent Interceptor代码,映射为“/”,因此我得到如下响应头:-

Cache-Control:no-cache
Cache-Control:no-store
Date:Wed, 20 Nov 2013 09:59:01 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified:Tue, 19 Nov 2013 10:07:02 GMT
Pragma:no-cache
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
我还尝试通过扩展AbstractController(而不是注释)来实现所讨论的控制器,但这也不起作用(与上面的标题相同)

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">

    <!-- Secures the application -->
    <filter>
        <filter-name>securityFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>springSecurityFilterChain</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>securityFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- Java-based Spring container definition -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <!-- Location of Java @Configuration classes that configure the components that makeup this application -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.blah.config com.blah.dataservice.repositories</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  <listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
  </listener>

    <servlet>
        <servlet-name>blah</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>blah</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>-1</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>/login</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>

</web-app>
控制器代码:-

package com.blah.controller.avatars;

import ...

@Controller
@SessionAttributes({ SessionModel.KEY })
@RequestMapping("/avatars")
public class AvatarListController {

    @Inject
    IProfilePicService profilePicService;

    @ModelAttribute
    public void formBacking(ModelMap model, HttpServletRequest request) {
        SessionModel instanceSessionModel = new SessionModel();
        instanceSessionModel.retrieveOrCreate(model);
    }

    @RequestMapping(value = "/{type}/{entityId}/{size}", method = RequestMethod.GET)
    public void getPic(@PathVariable String type, @PathVariable long entityId, @PathVariable String size, HttpServletResponse response, WebRequest request) throws IOException,
            InvalidKeyException, URISyntaxException, StorageException {

        Size sizeEnum = getSizeFromSizeString(size);

         long lastModified = profilePicService.getPicLastModifiedDate(entityId, type, sizeEnum);

        if (request.checkNotModified(lastModified)) {
            return;
         }

        InputStream is;

        is = profilePicService.getProfilePicInputStream(entityId, type, sizeEnum);


        OutputStream os = response.getOutputStream();
        IOUtils.copy(is, os);
        is.close();
        os.close();
    }
。。。
}

我有一个缓存问题,因此,我使用了以下方法。(禁用了头像图像的缓存。拦截方法对我也不起作用。)


图像是否来自您的应用程序?或者是在别的什么地方?另外,您是如何为这些图像提供服务的,您是否有自己的控制器,还是使用Springs资源配置(已为您提供缓存设置)。我们通过应用程序中的控制器为图像提供服务(它们存储在其他位置,但通过我们的应用程序路由)。我们使用的是控制器而不是Spring资源配置。您可以发布完整的配置(包括web.xml)吗。我猜你把拦截器连接到错误的配置上了。请张贴您的控制器代码。更新,谢谢您的帮助!确保您没有复制内容(即
)。乍一看,您的设置似乎没有什么奇怪的错误。谢谢,但这是否适用于从控制器提供的动态图像?
package com.blah.controller.avatars;

import ...

@Controller
@SessionAttributes({ SessionModel.KEY })
@RequestMapping("/avatars")
public class AvatarListController {

    @Inject
    IProfilePicService profilePicService;

    @ModelAttribute
    public void formBacking(ModelMap model, HttpServletRequest request) {
        SessionModel instanceSessionModel = new SessionModel();
        instanceSessionModel.retrieveOrCreate(model);
    }

    @RequestMapping(value = "/{type}/{entityId}/{size}", method = RequestMethod.GET)
    public void getPic(@PathVariable String type, @PathVariable long entityId, @PathVariable String size, HttpServletResponse response, WebRequest request) throws IOException,
            InvalidKeyException, URISyntaxException, StorageException {

        Size sizeEnum = getSizeFromSizeString(size);

         long lastModified = profilePicService.getPicLastModifiedDate(entityId, type, sizeEnum);

        if (request.checkNotModified(lastModified)) {
            return;
         }

        InputStream is;

        is = profilePicService.getProfilePicInputStream(entityId, type, sizeEnum);


        OutputStream os = response.getOutputStream();
        IOUtils.copy(is, os);
        is.close();
        os.close();
    }
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry
                .addResourceHandler("/resources/avatar/**")
                .addResourceLocations("/resources/avatar/")
                .setCachePeriod(0);

        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }