I heard that if you search the public account “Java Fish boy” on wechat, you will improve your skills to the next level

(1) Overview

SpringBoot is one of the most commonly used frameworks in today’s work. Most people only know how to use it. However, many details of SpringBoot have been neglected, so I plan to write a series of articles about SpringBoot. Try to make it something that both the novice and the veteran can learn from.

(2) Quickly build a SpringBoot project

Create a new project and select Spring Initialize. This is the SpringBoot quick setup portal. You can see that there is a default website here.Click this website, in fact, is a fast generation of Springboot project website, select the configuration here, click generate will automatically generate a compressed package, the package can be decompressed directly into idea.Of course, direct generation through IDEA will be more convenient. After clicking Next, the function of the next page is actually the same as that of the official website: fill in the project name, select language, Java version, etc. After selecting, click NextOn the next page, select the jars and versions of Springboot that you want to introduce. All Selected Dependencies are displayed in the Selected Dependencies section on the far right. Click Next and wait until the Maven Dependencies are installed.Finally, a complete project is generated, with all the dependencies selected during the configuration appearing in POM.xml. If you encounter problems that Maven cannot download, you are advised to change to a Maven image. Run the startup class directly, and the project starts successfully.

(3) Analysis of directory structure

Looking at the current directory structure, all code is placed under SRC /main/ Java, configuration files and resource files are placed under SRC /main/ Resources, and test code is placed under SRC /test. The target directory is the compiled class file directory, and all dependencies are placed in the pom.xml file.

Let’s take a look at the startup class, we can follow the source code to find that the startup class is actually a component. Go to the SpringBootApplication annotation.Click on the SpringBootConfiguration annotation to see the familiar configuration annotation, which is a Component

(4) the POM. XML

With Maven, we no longer need to manually import the JAR package, just configure the POM.xml file: let’s analyze the structure of the POMThe first tag represents that the project has a parent project, spring-boot-starter-parent. The rest of the names below are self-assigned when creating the project.The build tag at the bottom introduces the package plug-in, which we need to package the project as a JAR or war. Next comes the most important dependency import, where all dependencies introduced from the external repository are placed in Dependencies

<dependencies>
    <! -- Web dependencies: integrate tomcat, dispatchServlet, XML, etc.
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <! -- Unit tests -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
Copy the code

At this point I think you can easily create a SpringBoot project yourself.

(5) Pack

Earlier we introduced the packaging plug-in, which packages the newly created project directly. Double-click package directly to automatically package.If the package failed, you need to configure the package plug-in:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

Build Success will then be displayed, with an additional JAR package under the target package, which is the jar package generated by your project. The jar package can be run directly with the java-jar commandWouldn’t it be nice to put this jar on the server so that everyone can access your SpringBoot project?

(six) A little egg

When running the SpringBoot project, the first will come out a Spring logo, this logo can also be said to be SpringBoot small egg. Create a banner. TXT file in the same directory as application.properties and draw the banner you want to display.www.bootschool.net/ascii-art