繁体   English   中英

Spring Cache在doFilter中不起作用

[英]Spring Cache not working in doFilter

如果从doFilter调用,Spring Cache不起作用。

请注意,如果未从doFilter()调用,则Spring缓存将正常工作(例如,如果从rest服务调用)

如何在doFilter()中启用缓存? (也许在doFilter中不允许缓存?)

@Configuration
@EnableCaching
public class CredentialsInjectionFilter implements javax.servlet.Filter {

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(
                new ConcurrentMapCache("tenants")               
            ));
        return cacheManager;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
    throws IOException, ServletException {
        display(5);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Cacheable("tenants")
    public void display(int number) {
        // if cache working properly, code below will not execute after the first calling
        for(int i=0; i<50; i++) {   
               System.out.println("ramon called" +number);
        }
    }

如何扩展org.springframework.web.filter.GenericFilterBean并将缓存详细信息移到单独的服务类中

缓存服务

import java.util.Arrays;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

    @Component
    public class CacheService{

        @Bean
        public CacheManager cacheManager() {
            SimpleCacheManager cacheManager = new SimpleCacheManager();
            cacheManager.setCaches(Arrays.asList(
                    new ConcurrentMapCache("tenants")               
                ));
            return cacheManager;
        }


        @Cacheable("tenants")
        public void display(int number) {
            // if cache working properly, code below will not execute after the first calling
            for(int i=0; i<50; i++) {   
                   System.out.println("ramon called" +number);
            }
        }
    }

CredentialsInjectionFilter

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.filter.GenericFilterBean;

@Component
public class CredentialsInjectionFilter extends GenericFilterBean {

    @Autowired
    private CacheService cacheService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
    throws IOException, ServletException {
        cacheService.display(5);
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM