Implementation code of spring boot Hello World

Time:2020-1-13

This article is the introduction to springboot. Without any additional tools, we create a spring boot web project from scratch and run it.

Project construction

In the final analysis, spring boot is just a framework, a few jars, nothing magical. However, the process of creating a project with spring initializr blocks a lot of information, so it’s hard for us to understand the essence of spring boot. Next, only use Maven to build a spring boot web project from scratch.
First, create a maven empty project as shown below. The project name is spring boot hello.


<?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>

  <groupId>com.poype</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    
  </dependencies>
</project>

Now this is an empty Maven project. We can add the dependency we need in the dependencies tab, such as the dependency of spring boot. However, in order to reduce configuration and facilitate our development, spring boot provides a parent Maven project, spring boot starter parent. As long as our project inherits the spring boot starter parent project, we can reduce many configurations. Modify our POM configuration 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>

  <groupId>com.poype</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

  <dependencies>
    
  </dependencies>
</project>

At present, our Maven project has not imported any dependency, which can be implemented throughmvn dependency:treeCommand OK.
We are going to create a web project, so add the spring boot starter web dependency. Modify the POM configuration 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>

  <groupId>com.poype</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
</project>

Due to thedependencyManagementSpring boot starter web has been declared in, so we can omit its version configuration here.

Re executionmvn dependency:treeThe command gets the following results:


[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.poype:spring-boot-hello >---------------------
[INFO] Building spring-boot-hello 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ spring-boot-hello ---
[INFO] com.poype:spring-boot-hello:jar:1.0-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile
[INFO]  +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile
[INFO]  | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile
[INFO]  | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile
[INFO]  | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.4.RELEASE:compile
[INFO]  | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO]  | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO]  | | | \- org.slf4j:slf4j-api:jar:1.7.26:compile
[INFO]  | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile
[INFO]  | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile
[INFO]  | | \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile
[INFO]  | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO]  | +- org.springframework:spring-core:jar:5.1.6.RELEASE:compile
[INFO]  | | \- org.springframework:spring-jcl:jar:5.1.6.RELEASE:compile
[INFO]  | \- org.yaml:snakeyaml:jar:1.23:runtime
[INFO]  +- org.springframework.boot:spring-boot-starter-json:jar:2.1.4.RELEASE:compile
[INFO]  | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile
[INFO]  | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO]  | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
[INFO]  | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile
[INFO]  | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile
[INFO]  | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile
[INFO]  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.4.RELEASE:compile
[INFO]  | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.17:compile
[INFO]  | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.17:compile
[INFO]  | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.17:compile
[INFO]  +- org.hibernate.validator:hibernate-validator:jar:6.0.16.Final:compile
[INFO]  | +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO]  | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO]  | \- com.fasterxml:classmate:jar:1.4.0:compile
[INFO]  +- org.springframework:spring-web:jar:5.1.6.RELEASE:compile
[INFO]  | \- org.springframework:spring-beans:jar:5.1.6.RELEASE:compile
[INFO]  \- org.springframework:spring-webmvc:jar:5.1.6.RELEASE:compile
[INFO]    +- org.springframework:spring-aop:jar:5.1.6.RELEASE:compile
[INFO]    +- org.springframework:spring-context:jar:5.1.6.RELEASE:compile
[INFO]    \- org.springframework:spring-expression:jar:5.1.6.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.018 s
[INFO] Finished at: 2019-05-19T22:54:50+08:00
[INFO] ------------------------------------------------------------------------

You can see that after adding the dependency of spring boot starter web, many jars have been imported.

In addition to more spring boot starters-*Besides being imported, more importantly, many spring framework jars have also been imported, including spring core, spring beans, spring context, spring AOP, etc. In addition, Tomcat related jars have also been imported, which means that now we have servlet containers that can run web programs.

The project configuration has been completed. Create a new hellocontroller test class, and enter the following code:


package com.poype.springboot.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class HelloController {

  @RequestMapping("/hello")
  String home() {
    return "Hello World!";
  }

  public static void main(String[] args) {
    SpringApplication.run(HelloController.class, args);
  }
}

Now, we have completed a simple web application development and can start our application.

Since our project inherits the POM configuration of spring boot starter parent, it provides a plug-in to start the spring boot application (the run target of the plug-in is used to start the application), which can be executed bymvn spring-boot:runCommand to start the application and get the following results.

As you can see from the running results, spring boot starts the Tomcat server and listens on port 8080. Now let’s open the browser and enter the addresshttp://localhost:8080/helloYou can see the result of the program.

Application packaging

After the application is built, an application package needs to be built for production deployment. To do this, add a plug-in in the POM configuration. Modify the POM configuration 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>

  <groupId>com.poype</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Then we executemvn clean packageCommand, you can find the built application package in the target directoryspring-boot-hello-1.0-SNAPSHOT.jar

implementjava -jar ./target/spring-boot-hello-1.0-SNAPSHOT.jarCommand to start the application.

How does spring boot work

Although the above example is very simple, it is also a standard spring web application. Let’s recall that without spring boot, what steps are needed to create such a web application? The first step is to import n multi dependency (including spring’s, servlet’s, JSON’s…) into Maven’s POM. Then add various complex configurations (including servlet, spring, spring MVC…). Finally, write the code and build a war package. We need to download a Tomcat and put the war package in the specified path under Tomcat to start the Tomcat deployment application. Even for the old drivers who have worked for several years, it is estimated that it will take more than ten minutes to create a project from scratch. If the novice encounters some problems again, it will be more difficult to solve them. It may not be able to work out in a few hours.
Using spring boot to build applications, even if we only use maven, there is almost no configuration. If you use spring initializr, you can write code directly without any configuration after creating a project. It’s very convenient. Even a novice can create this HelloWorld application in a few minutes. This is the initial impression spring boot gave me, but how does it do this?

Convention is greater than configuration

Spring boot provides a lot of starter dependencies, and each type of starter provides a series of dependencies that this type of application may need (taking advantage of the indirect dependency feature of Maven). For example, what we create here is a web application, so our project depends on spring boot starter web, and spring boot starter web will help us import all the dependencies that web development may need, saving a lot of configuration work. Spring boot starter parent is a special starter that provides many Maven default configurations, such as dependency management.
Another important thing is annotation@EnableAutoConfigurationSpring boot will see this annotation and perform the relevant configuration according to the added jar dependency. For example, in our project, spring MVC and Tomcat depend on each other. Spring boot will guess that this is a web project, and it will perform the corresponding configuration for the project (such as the configuration of spring MVC and servlet).

Application startup

Spring boot comes with a Tomcat container, which saves us the work of installing and configuring the container ourselves. In order to understand the startup process of spring boot, we extract the directory structure from the built jar as shown in the following figure:

The main configuration file is manifest.mf, which executes Java jar*When. Jar starts the command, the JVM references the configuration of this file. The contents of the document are as follows:


Manifest-Version: 1.0
Implementation-Title: spring-boot-hello
Implementation-Version: 1.0-SNAPSHOT
Built-By: poype
Implementation-Vendor-Id: com.poype
Spring-Boot-Version: 2.1.4.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.poype.springboot.web.HelloController
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_211
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
 ot-starter-parent/spring-boot-hello

Main class is the startup class in the jar package. You can see that it is an org.springframework.boot.loader.jarlauncher class, which is provided by spring boot. Start class is a class with main method written by ourselves. Spring boot classes is the path of the application’s own classes, and spring boot lib is the path of the third-party package that the application depends on.

As you can see here, we can roughly summarize (just YY, at present, the understanding is not deep enough). Spring boot implements a set of its own deployment path specifications (where to put the application’s own classes, where to put the third-party jar on which the application depends, etc.), just like the J2EE specification. Then use Tomcat’s jar to realize the function of servlet container to process the web request.

It can be said that spring boot uses Tomcat to build a new platform. This platform only uses Tomcat’s functions in the servlet container part. As for the deployment specifications and loading mechanisms, spring boot implements them completely.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.