Spring integration with springmvc
The bean definition loaded in spring is loaded by the ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory() method in the org.springframework.context.ConfigurableApplicationContext#refresh method. This method then calls the org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory method to create a bean factory and load the bean definition. <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Load the spring container--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure the configuration file that springMVC needs to load--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <!-- Matches all requests by default --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> When the tomcat container is started, when accessing resources through the path, the org.springframework.web.servlet.HttpServletBean#init method will be called for the first time, and subsequent http requests will no longer use this method class; HttpServletBean implements the specification of the Servlet interface, so when the browser request is initialized and executed through the servlet interface, the loading class defined in the springmvc configuration will be loaded from the spring container. Spring and springmvc are parent-child containers. The following is the init method of HttpServletBean public final void init() throws ServletException { // Set bean properties from init parameters. PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); if (!pvs.isEmpty()) { try { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); 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; } } //Finally, the refresh method of the org.springframework.context.ConfigurableApplicationContext#refresh container will be called. // Initialize the springmvc container initServletBean(); } } Springboot starts the container
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors method, in which the bean definition is loaded through the ConfigurationClassPostProcessor class, which implements the BeanDefinitionRegistryPostProcessor interface, which allows the processing of bean definitions. // The BeanDefinitionRegistryPostProcessor in spring is a subinterface of BeanFactoryPostProcessor. // The function of BeanFactoryPostProcessor is to execute the postProcessBeanFactory() method when the bean definition information has been loaded but not initialized. // The BeanDefinitionRegistryPostProcessor is executed before the BeanFactoryPostProcessor. The execution order is defined in the source code // org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors() method // BeanFactoryPostProcessor is the bean property processing container of the bean factory. In simple terms, it can manage all beandefinition (uninstantiated) data in our bean factory and can modify properties at will. public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { prepareRefresh(); //Get and tell the subclass to initialize the Bean factory to load the beans into the cache. Spring Spring MVC integration is to initialize the bean here. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); prepareBeanFactory(beanFactory); try { postProcessBeanFactory(beanFactory); // The springboot container starts loading at this point and initializes the following bean names //0 = "org.springframework.context.annotation.internalConfigurationAnnotationProcessor" =》Corresponding ConfigurationClassPostProcessor class //1 = "org.springframework.context.annotation.internalAutowiredAnnotationProcessor" =》AutowiredAnnotationBeanPostProcessor //2 = "org.springframework.context.annotation.internalCommonAnnotationProcessor" =》CommonAnnotationBeanPostProcessor //3 = "org.springframework.context.event.internalEventListenerProcessor" =》EventListenerMethodProcessor //4 = "org.springframework.context.event.internalEventListenerFactory" = "DefaultEventListenerFactory // Call the postprocessor of our bean factory. Load the bean definition (not instantiation), load the scan path in the startup class through ConfigurationClassPostProcessor // Then load the path to the bean invokeBeanFactoryPostProcessors(beanFactory); registerBeanPostProcessors(beanFactory); initMessageSource(); initApplicationEventMulticaster(); // This method is also implemented by a subclass of Springboot, which also starts tomat from this method. onRefresh(); registerListeners(); //Instantiate the rest of our singleton beans. finishBeanFactoryInitialization(beanFactory); // Finally, the container refreshes and publishes the refresh event (Spring cloud is also started from here) finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } } This is the end of this article about the difference between traditional tomcat startup service and springboot startup built-in tomcat service. For more information about the difference between tomcat startup service and springboot startup built-in tomcat service, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Is a design that complies with design specifications a good design?
Preface: When we want to clear a table, we often ...
This article example shares the specific code of ...
What is Virtual Memory? First, I will directly qu...
Problem: The PHP program on one server cannot con...
Automatic web page refresh: Add the following code...
A simple record of the database startup problems ...
I have been quite free recently. I have been doin...
Linux installation MySQL notes 1. Before installi...
Table of contents 1 The common rules for creating...
1. Purpose Write a Flask application locally, pac...
The HTTP request methods specified by the HTTP/1....
Table of contents 1. Sample code 2. See the essen...
Table of contents 1. Project folder structure 1. ...
After entering yum in linux, the prompt: -bash: /...
Table of contents 1. Why do we need vue3? 2. Adva...