欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

web过滤器使用spring依赖注入 解决过滤器中不能注入bean的问题

程序员文章站 2022-03-05 14:15:54
...
@Component
public class AppGateAwayFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(AppGateAwayFilter.class);
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
}
 
@Configuration
public class Config implements WebApplicationInitializer {
 
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
        delegatingFilterProxy.setTargetBeanName("appGateAwayFilter");
        delegatingFilterProxy.setTargetFilterLifecycle(true);
        FilterRegistration filterRegistration = servletContext.addFilter("appGateAwayFilter",delegatingFilterProxy);
        filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/rest/api/*");
    }
 
}