Learning journey of Java EE spring boot

Why use SpringBoot

SpringBoot improves and optimizes the above shortcomings of Spring, based on the idea that convention is better than configuration, which allows developers to devote themselves to the code writing of logical business without switching between configuration and logical business, thus greatly improving the efficiency of development
Efficiency, to a certain extent, shorten the project cycle.

Features of SpringBoot

  • Provide a faster entry experience for Spring based development
  • Out of the box, no code generation, no XML configuration. At the same time, you can modify the default value to meet specific needs
  • It provides some common non functional features in large projects, such as embedded server, security, indicators, health detection, external configuration, etc
  • SpringBoot is not an enhancement to the function of Spring, but a quick way to use Spring

Build SpringBoot project

Method 1: Official Website Creation

visit https://start.spring.io/ Create, as shown in the figure below:

Click GENERATE to GENERATE and download the project package. After downloading, unzip the project file and open it with IDEA.

Method 2: IDEA creation



Method 1: ordinary Maven project creation

SpringBoot itself is a Maven project. The above tools only generate some default content. For example, the following project structure is generated for the tool (we can manually create the folder ourselves)

In the above part, pom.xml can be set according to the required dependency package (we need to import the dependency package ourselves), and it can be generated manually by ourselves, so we don't have to use tools.

It should be noted that the embedded server Tomcat 9.0.39 is introduced into SpringBoot. In order to improve the performance, this version of Tomcat embedded server will load the local library (the library file needs to be downloaded) and use the API of JDK9 or above. It will report an error (exception) at startup. Although it does not affect the use, the experience is not very good. We can use another server, Undertow, which is a flexible and high-performance Web server developed by red hat with Java

  • 1. Fully embedded Web server, you can start a Web server directly by using API.
  • 2. Fully compatible with Java EE Servlet 4 and low-level non blocking processor.
  • 3. Provide blocking and NIO based non blocking mechanisms.
    In the system with high concurrent requests, according to the performance test data, Undertow performs better than Tomcat in memory and performance. At present, many projects using SpringBoot replace the default embedded Tomcat server with Undertow.
    It's very convenient to change the Web server in SpringBoot. You just need to adjust the Maven dependency, remove the default embedded Tomcat server, and add the Undertow server. It's no different from Tomcat in use.

pom.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- Default Spring Framework Version 5.2.10.RELEASE -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>springboot-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- spring-boot-starter-web: be based on SpringBoot Developed dependency packages,
                                 Will be dependent again spring-framework Basic dependency package in, aop Dependent packages, web Dependent packages,
                                 There will also be other applications such as json,tomcat,validation Equal dependence -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- exclude tomcat rely on -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- add to Undertow rely on -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

        <!--introduce AOP rely on-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- spring-boot-devtools: SpringBoot Hot deployment dependency package for -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <!-- Can not be inherited by other modules, if multiple sub modules can be removed -->
            <optional>true</optional>
        </dependency>

        <!-- lombok: simplify bean Framework of code -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- spring-boot-starter-test: SpringBoot Test framework -->
        <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>

    <build>
        <plugins>
            <!-- SpringBoot Of maven Package plug-ins -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Specify the version of some plug-ins to avoid being affected maven The impact of version -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>

        </plugins>
    </build>

</project>

Web development instruction based on SpringBoot project

Embedded Web server

SpringBoot has integrated Tomcat. In the future, you don't need to download a Tomcat program like traditional Web development and configure it to start. Just start the generated startup class.

Default Web resources folder

SpringBoot will use the following folder in src/main/resources directory as the Web resource folder by default:

  • 1./static: static resource folder, such as html, js, css, etc
  • 2./public: static resource folder, the same as above
  • 3./templates: template resource folder (the template file used by the back-end template framework will parse variables and generate dynamic html)

Note: for the resources in the above paths, the service path does not need to be preceded by / static, / public. For example, the service path of home.html in the static folder is / home.html (you can directly enter / home.html without adding / static)

If you are building with an ordinary Maven project, you can create the above folder by yourself, and create the public and static folder under src/main/resources.

Default profile

Spring boot uses src/main/resource/application.properties as the startup configuration file by default. You can customize the configuration such as startup port.

If you are using an ordinary Maven project, you can create application.properties in src/main/resources and leave the content blank (you can add some content as needed, such as tomcat startup port and database connection settings).

Default application context path

For Web projects launched by SpringBoot, the application context path is/

Startup class

If you are building with an ordinary Maven project, you can write your own startup class: use @ SpringBootApplication annotation on the class:

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//Spring boot boot class annotation
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        // Note that the first parameter is the class object of the current class
        SpringApplication.run(Application.class, args);
    }
}

The configuration to be modified is as follows:

Verify Web static resources

  • In the static or public directory of src/main/resources, create an html web page and visit it.

  • Start Application startup class
  • Visit index.html

Tags: Java Spring Maven JavaEE Tomcat Spring Boot

Posted by php_tom on Sat, 12 Jun 2021 07:29:23 +0930