过滤器是servlet规范中定义的,并不归spring容器管理,也无法直接注入spring中的bean(会报错)
初始化时通过spring上下文获取,进行bean的初始化:
@Overridepublic void init(FilterConfig filterConfig) throws ServletException { ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext()); RedisTemplate demoBean = (RedisTemplate)context.getBean("redisTemplate"); System.out.println(demoBean); }