How to inject Service and javafilter in Java Filter

Source: Internet
Author: User
Tags openid

How to inject Service and javafilter in Java Filter

A problem occurs in the project. If Serivce injection fails in the Filter, the injected service is always null. As follows:

public class WeiXinFilter implements Filter{        @Autowired    private UsersService usersService;

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
     Users users = this.usersService.queryByOpenid(openid);
}

The above usersService will report a null pointer exception.

Solution:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        HttpServletRequest req = (HttpServletRequest)request;        HttpServletResponse resp = (HttpServletResponse)response;        ServletContext sc = req.getSession().getServletContext();
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
        
        if(cxt != null && cxt.getBean("usersService") != null && usersService == null)
            usersService = (UsersService) cxt.getBean("usersService");
        
        Users users = this.usersService.queryByOpenid(openid);

That's all.

Method 2:

public class WeiXinFilter implements Filter{        private UsersService usersService;        public void init(FilterConfig fConfig) throws ServletException {        ServletContext sc = fConfig.getServletContext();         XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);                if(cxt != null && cxt.getBean("usersService") != null && usersService == null)            usersService = (UsersService) cxt.getBean("usersService");            }

Related Principles:

1. How to obtain ServletContext:

1) Get it directly in javax. servlet. Filter
ServletContext context = config. getServletContext ();

2) directly retrieve from HttpServlet
This. getServletContext ()

3) Use HttpServletRequest to obtain
Request. getSession (). getServletContext ();

2. WebApplicationContext and ServletContext(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.