Using some beans in Filter bean class?

后端 未结 5 2197
执笔经年
执笔经年 2020-12-06 00:35

In my filter bean class, I added some beans dependency (with @Autowired annotation). But in the method doFilter(), all my dependency beans have nul

5条回答
  •  时光说笑
    2020-12-06 01:17

    Look at this answer on site of spring: http://forum.springsource.org/showthread.php?60983-Autowiring-the-servlet-filter

    In brief - you can manually force spring to apply @Autowire annotation to your filter:

    public void init(FilterConfig filterConfig) throws ServletException {
    
        ServletContext servletContext = filterConfig.getServletContext();
        WebApplicationContext webApplicationContext = 
                WebApplicationContextUtils.getWebApplicationContext(servletContext);
    
        AutowireCapableBeanFactory autowireCapableBeanFactory =
               webApplicationContext.getAutowireCapableBeanFactory();
    
        autowireCapableBeanFactory.configureBean(this, BEAN_NAME);
    }
    

提交回复
热议问题