springboot在配置的filter类中注入bean

@WebFilter(urlPatterns = "/*")
@Order(Integer.MAX_VALUE-2)
public class SSOFilter implements Filter {
   // @Value("${allowUrls}")
    //private String str;
    private static String[] allowUrls;
    //@Value("${redirectUrl}")
    //private String redirectUrl;
    private static String REDIRECT_URL;
   // @Value("${ssoVerifyUrl}")
    //private String ssoVerifyUrl;
    private static String SSO_VERIFY_URL;

    private static Logger logger = LoggerFactory.getLogger(SSOFilter.class);
    private static RedisTemplate redisTemplate;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext servletContext = filterConfig.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        redisTemplate = ctx.getBean("redisTemplate",RedisTemplate.class);
        logger.info("[redis对象],初始化...");

     }

}

注:  此方法不能用注解的方式给redisTemplete 赋值。原因:加@WeFilter的filter类在spring容器初始化之前加载

而@Autowired或@Resource注入的时机是在spring容器初始化之后。

你可能感兴趣的:(springboot)