HttpServletBean is a servlet base class in Spring MVC that directly inherits the HttpServlet class and overwrites the init() method
Those of you who know servlets know the three most important methods of servlets
- When the servlet container creates a servlet object, it calls its init() method to initialize it
- The service() method handles the request, whether you’re a POST or get request, and ultimately the service() method handles the request
- You can use the destroy() method to close the file stream or close the database connection
So what does HttpServletBean do when it overwrites init()
- Get the Spring MVC configuration of init-param in servletConfig and encapsulate it into the PropertyValues object
- Convert the servlet class to BeanWrapper to allow Spring to better manipulate Javabean properties
- Customize the ResourceLoader editor
- A call to the initServletBean method is implemented by its subclass FrameworkServlet, which initializes the webApplicationContext container
@Override public final void init() throws ServletException { // Set bean properties from init parameters. / / parsing init param - encapsulated into PropertyValues object PropertyValues PVS = new ServletConfigPropertyValues (getServletConfig (), this.requiredProperties); if (! Pvs.isempty ()) {try {/** * convert the servlet class to BeanWrapper, which spring uses to manipulate Javabeans. Using BeanWrapper * can directly to the javabean property assignment * / BeanWrapper bw = PropertyAccessorFactory. ForBeanPropertyAccess (this); / / custom properties editor ResourceLoader ResourceLoader = new ServletContextResourceLoader (getServletContext ()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); // Empty method initBeanWrapper(bw); bw.setPropertyValues(pvs, true); } catch (BeansException ex) { if (logger.isErrorEnabled()) { logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex); } throw ex; }} // Let subclasses do whatever initialization they like. // Let subclasses implement initServletBean(); } // The configuration loaded into Spring MVC encapsulates the PropertyValue object //requiredProperties to indicate that certain properties must exist. Used to verify configuration public ServletConfigPropertyValues (ServletConfig config, Set<String> requiredProperties) throws ServletException { Set<String> missingProps = (! CollectionUtils.isEmpty(requiredProperties) ? new HashSet<>(requiredProperties) : null); Enumeration<String> paramNames = config.getInitParameterNames(); while (paramNames.hasMoreElements()) { String property = paramNames.nextElement(); Object value = config.getInitParameter(property); addPropertyValue(new PropertyValue(property, value)); if (missingProps ! = null) { missingProps.remove(property); } } // Fail if we are still missing properties. if (! CollectionUtils.isEmpty(missingProps)) { throw new ServletException( "Initialization from ServletConfig for servlet '" + config.getServletName() + "' failed; the following required properties were missing: " + StringUtils.collectionToDelimitedString(missingProps, ", ")); }}Copy the code