Springmvc startup process

Spring is loaded with a DispatcherServlet.

If load-on-startup is set, it will be loaded when the servlet is initialized. Init () is called when the servlet is loaded.

A refresh() method is then called, which is ostensibly used to refresh but is also used to initialize the bean

The next thing you need to know is how to initialize the ApplicationContext. If you get the ApplicationContext, you get the bean information, so when you initialize the ApplicationCotext, It has already initialized the bean’s information, or at least its path, and its description, so once we know how ApplicationCotext is initialized, we basically know how the bean is loaded.

The XmlWebApplicationContext or and ClassPathXmlApplicationContext inherited classes (indirect inheritance), AbstractApplicationContext this class the refresh () method is common, The method they both call to load the bean, in which case the beanFactory is constructed using the obtainFreshBeanFactory method

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
Copy the code

1, do not skip the source code comments

2, don’t start on details, first understand the context of the source code, familiar with the details

3. Take a wild guess

4, according to the name of the method, know what is the method

5, insist on looking at the source code

Based on reflection

springioc

Spring startup?? See Ma Shibing

The life cycle of SpringIOC: 1. Create beanFactory first, initialize the factory,

2. Beandefinition reads the configured file to get the beandefinition information

The bean factory processor further parses the bean definition information, such as ${name}, converts it to a value, or resets the bean property value

Beanfactory instantiates the bean

5. The populate method populates the instantiated object properties

The heart aware interface needs to execute methods to get properties of other beans

7. Preprocessor for bean initialization

Initialize the bean

9. Bean post-processor (where springaop is inserted into the bean)

10. The bean is created

11. Bean destruction

Ioc parses configuration files and implements methods through interface specifications

When used, objects are instantiated by reflectionThe object creation process includes: instantiate (memory space) — “object fill property set method –> initialize (action that can be done before creating the object) –> create the object

Before spring beanfactoryPostprocessor can create bean definition of bean processing Before defines the bean’s information to the beanfactory beanfactoryProatprocessor, used for processing parsing bean definition information in advance, The following figure reads the database user name

The Aware class is used to make it easy for bean objects to implement the methods that the Aware interface needs to implement after they get the bean properties in the container to populate the properties

Beanpostp ‘RocessorBefore and BeanPostp ‘Rocessorbefore are the actions before the object is created

Create beanfactory refreshBeanFactory (if there is one, destroy it and create a new factory)

2. The LoadBeanDefinition method reads the configuration file to complete the factory creation and bean configuration file loading

3, invokBeanFactoryPostProcesor invoke various bean processors

The registration work below the bean processor is for later implementation

Then instantiate the object

Polulatbean populates the property values of the bean

The InvokawareMethod method executes the Aware method

Why does a Java implementation of springaop need an interface? The proxy class needs to inherit invokHandler to execute the promenade method of the parent class. Because the interface is implemented, the generated proxy class can be implemented, which is equivalent to the interface of the static proxy

That is, a dynamic proxy uses reflection to generate a class that is similar to a static proxy (the class is a subclass of the target method). This class inherits the InvokHandler, and can call the target method of the parent class. It inherits the InvokHandler, so that the target method of the parent class can implement multiple interfaces through reflection. This interface is implemented in conjunction with the invokHandler inheritance so that when a method is invoked, the method behind the proxy can be invoked

Spring loop dependencies

Defaltsingaltone Level 3 cache

@aspectj

Springaop’s rationale: if you don’t actively set parameters or implement an interface, use the JDK’s dynamic proxy, otherwise use cglib

He is after initialization bean object within a post processor applybeanpostprocessorafterinitalition weave In front of bean, without using an interface, or specify configuration is true, Use the Cglib proxy or use the JDK dynamic proxy getProxyNewinstance

Object creation for the Spring container

Object creation methods: 1, new 2, reflection 3, create design pattern

For springmvc process

The front controller is a servlet

Process description: 1. The client sends the request to dispatchServlet (front-end controller). 2. Processor executor executes handler (processor) according to the execution chain obtained by DispatchServlet. 4. Processor encapsulates the final result of execution into ModelAndView. Returned to the front controller (dispatchservlet) 5, front controller calls to the parser (we configuration), returns a View object to the front controller (dispatchservlet) 6, the front controller calls trying to (HTML | Freemark), Render the parsed VEW to try 7. Try to respond back to the client

The main things springBoot does are: 1, embedded Tomcat 2, automatic assembly. Springboot source code parsing

webapplicationinnalization

The implementation principle of spring@configration:

Because of a new specification in the servlet3.0 API: If a class or method needs to be called when the container is started, you can specify a file in the root directory of the project startup. META-/services onstartup If @configration is registered in the spring container, the onstartup method will be executed. If @configration is registered in the spring container, the onstartup method will be executed. At the same time, through the transmission of servletCXt, register dispatcServlet, complete the integration of SpringMVC

Why is the Interface developed by Spring invoked when the Web container is started?

Springboot needs to implement this interface, and Tomcat can call the onStartup method inside

The inside of the onstartup method annotationconfigwebapplicationcontext method into the spring container entrance in the form of annotation classpathxmlapplicationcontext (initial words spring container) This class can register configuration classes (@configration) as shown in the following figure:

The Web container passes the ServletCXt in and completes the springMVC integration by creating a DispatcServlet and adding it to the ServletCXt

@AutoConfigRationPackage: Automatic package scanning subdirectory class SpringBoot automatic configuration class SpringFactory configured with all the default configuration classes Spring automatic configuration core: Through the import injection autoconfigrationimportselector importselector will scan the META – Spring. Factories of the configuration file (it has all the default configuration class), getAutoconfigration get all automatic configuration of nodes, getCandidateconfigrations according to scan the document filtering, Loadfactorynames scans all auto-configuration classes in the configuration file and initializes them in the container

Integration of Springboot and other projects requires the import of a xxxx-starter project,

How does SpringBoot embed web containers

Springboot has built-in Tomcat, Jetty and Undertow containers

Tomcat is automatically configured by SpringBoot

Why is SpringBoot a Java project and not a Web project? Because SpringBoot uses the configuration method, the web. XML file is not required, whereas Tomcat parses its web. XML file for a given Web project

The bottom layer of SpringMVC is servlet technology, so the DispatchServerlet of SpringMVC is created and stored in the Web container before it can be used in Tomcate. There are SpringMVC Tomcat and Spring, which are both run on JDK. You could say it’s a dependency

Tomcat is injected into the Spring container

Springboot has a tomcat dependency by default

Next, create Tomcat

Spring Boot is automatically configured after the SpringIOC container is initialized

To use Tomcat embedded in springBoot, you can create a class and add @configration to configure the Tomcat attribute, or configure the tomcat attribute in the YML file. To use other Web containers, exclude the built-in Tomcat and import the starter package

Springboot’s run method

The main method is used to receive command-line arguments such as java-jar-xx.jar

Netty source

Dubbo source

Zookepper source