在Filter中注入Service

贴一段代码简单明了:

@WebFilter(filterName = "MainFilter",urlPatterns = "/*")
public class MainFilter implements Filter {

    private UserService userService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ApplicationContext ctx = WebApplicationContextUtils
                .getRequiredWebApplicationContext(filterConfig.getServletContext());
        userService = ctx.getBean(UserService.class);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
           filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

就这样,userService就可以正常使用了

上一篇:SpringBoot框架基础知识(1)


下一篇:Spring MVC Controller 接收ajax的数组参数说明