This article is excerpted from Spring 5 core principles
1. Idea integrated Lombok plug-in
1.1 installing plug-ins
IntelliJ IDEA is an excellent integrated development tool with powerful functions and many plug-ins. Lombok is an open source code generation library and a very practical gadget. When editing entity classes, we can use Lombok annotations to reduce the writing of getter s, setter s and other methods. When changing entity classes, we only need to modify properties, which reduces the writing of a lot of duplicate code. First, you need to install the Lombok plug-in in IntelliJ IDEA. After opening IntelliJ IDEA, click File → Settings in the menu bar (as shown in the figure below), or use the shortcut key Ctrl+Alt+S to enter the setting interface.
Click Plugins in the settings interface to install the plug-in, and click Browse repositories on the right, as shown in the following figure.
Then enter lombok in the search interface to query the lower Lombok Plugin. Click Lombok Plugin to see the Install button on the right. Click this button to start the installation, as shown in the following figure.
We can see all the annotations supported by Lombok in the following installation interface.
There is a prompt of Downloading Plugins during installation, and the progress bar will change during installation. It should be reminded that the network connection must be available and good during the installation process, otherwise the installation may fail. After successful installation, you can see the Restart IntelliJ IDEA button on the right side of the figure below. At this time, you can not operate first because there are subsequent configuration work.
Back to Plugins, Lombok can be found on the right side of the figure below (but not before installation).
1.2 configure annotation processor
In the following setting interface, click Build, Execution, Deployment → Compiler → Annotation Processors, and then check Enable annotation processing on the right.
1.3 using plug-ins
Before use, it should be noted that the installed plug-in is just a call. Just as we use the maven plug-in, maven needs to be installed on the machine. We also need to use POM. Com before using Lombok Add Lombok dependencies to the XML file.
#Managed class scan package path# <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
Next, edit a Config test class, add two attributes, and finally add the @ Data attribute on the class. This annotation can help us in Generate the get of all properties in the class in the class file /set, equals, canEqual, hashCode, toString methods, etc., as shown in the following figure.
We can also view Lombok generated methods in the following way. Click View → Tool Windows → Structure in the menu bar to see all the methods in the class, which are automatically generated by Lombok, as shown in the following two figures.
2 from Servlet to ApplicationContext
In the chapter "extracting the core principles of Spring by handwriting with 300 lines of code", we have learned that the entry of Spring MVC is DispatcherServlet, implemented the init() method of DispatcherServlet, and completed the initialization of IoC container in the init() method. In the process of using Spring, the most common is ApplicationContext. It seems that all instance beans managed by Spring can be obtained by calling the get Bean () method. So where does ApplicationContext come from? As you can see from the Spring source code, the class diagram of DispatcherServlet is shown in the figure below.
Dispatcher Servlet inherits frameworkservlet, frameworkservlet inherits httpservletbean, and httpservletbean inherits HttpServlet. In the init() method of HttpServletBean, the initServletBean() method of FrameworkServlet is invoked, and the WebApplicationContext instance is initialized in the initServletBean() method. In the initServletBean() method, the DispatcherServlet (onRefresh) method is overridden. initStrategies() method is called in onRefresh() method of dispatcherservlet to initialize nine components of Spring MVC. In fact, through the above complex call relationship, we can draw a conclusion: the init() method of Servlet initializes the nine components that IoC container and Spring MVC depend on. Draw the frame class relationship before handwriting, as shown in the figure below. By the way, review the IoC container structure we talked about before.
3 prepare basic configuration
Before we start handwriting, let's make an agreement. All handwriting classes start with GP, which is different from the native classes in the Spring framework for comparison and understanding. For example, DispatcherServlet will be named GPDispatcherServlet in this Mini version. All method names should be consistent with the native Spring as far as possible. Some parameter lists may be fine tuned to understand the design idea.
3.1 application.properties configuration
Or start with application Start with the properties file and use application Properties instead of application XML. The specific configuration is as follows:
#Managed class scan package path# scanPackage=com.gupaoedu.vip.spring.demo
3.2 pom.xml configuration
Next, look at POM XML configuration, mainly focusing on jar package dependency:
<properties> <!-- dependency versions --> <servlet.api.version>2.4</servlet.api.version> </properties> <dependencies> <!-- requied start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.api.version}</version> <scope>provided</scope> </dependency> <!-- requied end --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies>
3.3 web.xml configuration
web. The XML configuration is as follows:
<?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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Gupao Spring Application</display-name> <servlet> <servlet-name>gupaomvc</servlet-name> <servlet-class>com.gupaoedu.vip.spring.formework.webmvc.servlet.GPDispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>gupaomvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
3.4 GPDispatcherServlet
The gpdispatcher servlet code is as follows:
package com.gupaoedu.vip.spring.formework.webmvc.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; //Servlet is only used as a startup entry for MVC public class GPDispatcherServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
This is the end of the preparation stage of writing a Spring with 30 classes. We must remember to do it ourselves. You can't practice fake moves just by watching.
Pay attention to WeChat official account Tom bomb structure and reply to "Spring" to get the complete source code.
This article is the original of "Tom bomb architecture". Please indicate the source for reprint. Technology lies in sharing, I share my happiness!
If this article is helpful to you, you are welcome to pay attention and praise; If you have any suggestions, you can also leave comments or private letters. Your support is the driving force for me to adhere to my creation. Focus on WeChat official account Tom structure, get more dry cargo!
It's not easy to be original. It's cool to insist. I've seen it here. Little partners remember to like, collect and watch it. Pay attention to it three times a button! If you think the content is too dry, you can share and forward it to your friends!