Configure Spring in web. xml as follows:
<Servlet>
<Servlet-name> springServlet </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/applicationContext. xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
In the Servlet, use WebApplicationContextUtils. getWebApplicationContext (getServletContext () to obtain that the WebApplicationContext object is null. This is because in addition to DispatcherServlet configuration, ContextLoaderServlet needs to be configured; otherwise, WebApplicationContext cannot be obtained. The configuration method is as follows:
<Servlet>
<Servlet-name> context </servlet-name>
<Servlet-class> org. springframework. web. context. ContextLoaderServlet </servlet-class>
<Load-on-startup> 2 </load-on-startup>
</Servlet>