A problem was encountered in the project, and injecting Serivce failed in the filter, and the injected service is always null. As shown below:
Public class Implements filter{ @Autowired private usersservice usersservice;
The Filterchain times null pointer exception is used in the public void DoFilter (ServletRequest request, servletresponse response, Chain Usersservice) method.
Workaround:
Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {httpservletrequest req=(httpservletrequest) request; HttpServletResponse resp=(httpservletresponse) response; Xmlwebapplicationcontext Context=(Xmlwebapplicationcontext) webapplicationcontextutils.
Getwebapplicationcontext (Req.getsession (). Getservletcontext ()); if(Context! =NULL&& Context.getbean ("Usersservice")! =NULL&& Usersservice = =NULL) Usersservice= (Usersservice) context.getbean ("Usersservice"); Users Users= This. Usersservice.querybyopenid (OpenID);
That's all.
How to inject Service in Java Filter