Java Spring缓存在doFilter中不工作
如果从doFilter调用,则Spring缓存不工作 请注意,如果不是从doFilter()调用,Spring缓存将工作(例如,如果是从rest服务调用) 如何在doFilter()中启用缓存?(doFilter中可能不允许缓存?)Java Spring缓存在doFilter中不工作,java,spring,spring-mvc,caching,Java,Spring,Spring Mvc,Caching,如果从doFilter调用,则Spring缓存不工作 请注意,如果不是从doFilter()调用,Spring缓存将工作(例如,如果是从rest服务调用) 如何在doFilter()中启用缓存?(doFilter中可能不允许缓存?) @配置 @启用缓存 公共类CredentialsInjectionFilter实现javax.servlet.Filter{ @豆子 公共缓存管理器缓存管理器(){ SimpleCacheManager cacheManager=新的SimpleCacheManag
@配置
@启用缓存
公共类CredentialsInjectionFilter实现javax.servlet.Filter{
@豆子
公共缓存管理器缓存管理器(){
SimpleCacheManager cacheManager=新的SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(
新的ConcurrentMapCache(“租户”)
));
返回缓存管理器;
}
@凌驾
public void doFilter(ServletRequest ServletRequest、ServletResponse ServletResponse、FilterChain FilterChain)
抛出IOException、ServletException{
显示器(5);
doFilter(servletRequest,servletResponse);
}
@可缓存(“租户”)
公共无效显示(整数){
//如果缓存工作正常,则在第一次调用后将不会执行下面的代码
对于(inti=0;i如何扩展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);
}
}
}
您正在将@Cacheable应用于一个不返回任何内容(void返回类型)的方法。在这种情况下,您希望发生什么?
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);
}
}
}
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);
}
}