I started to write Spring Boot-related blogs at the end of 2016, when the version was still 1.4.x. My article was published on CSDN, and the most read one was 43W+, as shown below:

In 2017, due to various reasons, I stopped updating my blog about Spring Boot. In 2018, I wrote a book and did not update it. Now the latest stable version of Spring Boot is 2.1.8, and Songo wants to write a series of tutorials about it. Spring Boot2. Let’s start this series with this one.

Spring the Boot is introduced

When we first began to learn JavaWeb, we used Servlet/JSP to do development, one interface to make a Servlet, very large, later we can reduce the creation of Servlet by hiding the domain or reflection and other ways, but it is still inconvenient, and then, We introduced Struts2/SpringMVC framework to simplify our development, compared with Servlet/JSP, after the introduction of framework, productivity did improve a lot, but after a long time, we found new problems, that is, cumbersome configuration prone to error, to do a new project, first set up the environment, The environment is built to build, just a few lines of configuration, different projects, may be different packages, most of the other configuration is the same, Java is always criticized for cumbersome configuration and large code, this is one of the manifestations. So what do we do? Spring Boot emerges as The Times require. Spring Boot mainly provides the following functions:

  1. Provide a quick and easy start for all Spring-based Java development.
  2. Out of the box, if you have your own custom configuration, use the official default.
  3. It provides a series of generic non-functional functions, such as embedded server, security management, health detection, and so on.
  4. There is absolutely no code generation, and no XML configuration is required.

Spring Boot made Java development easy again, because it really solved the pain point of development, so the technology was widely used. Many of Songo’s friends went out to interview Java engineers, and since the beginning of 2017, Spring Boot has been almost mandatory. The popular Spring Cloud microservices are also based on Spring Boot, so it is essential for all Java engineers to master Spring Boot.

System requirements

As of this writing (2019.09), the latest version of Spring Boot is 2.1.8, which requires at least JDK8. The integrated Spring version is 5.1.9, and the build tool version requirements are as follows:

Build Tool Version
Maven 3.3 +
Gradle 4.4 +

The built-in container versions are as follows:

Name Version
Tomcat 9.0 4.0
Jetty 9.4 3.1
Undertow 2.0 4.0

There are three creation methods

Spring Boot project is essentially a Maven project. From this point of view, Songo will introduce three ways to create a project.

Create online

This is one of the official creation methods. In fact, if we use the development tools to create a Spring Boot project (the second option), it is also created from this site, but the process development tools help us to do this, we only need to do a simple configuration in the development tools.

First go to https://start.spring.io, as follows:

The configurations are as follows:

  • Is the project build tool Maven or Gradle? Gradle is used a lot in Android. Gradle is used a lot in The Java back end. Maven is the main Java back end.
  • The development language, of course, is Java.
  • As you can see, the latest stable version is 2.1.8, which is what we are using here.
  • If you want to create a Maven project, you will need to specify the project coordinates, project description, etc.
  • Packing means whether the project should be packaged as a JAR package or a WAR package. One of the advantages of Spring Boot is that the Servlet container is embedded, which can be run directly after being packaged as a JAR package. Therefore, it is recommended to package the project as a JAR package. The developer can also choose the WAR package depending on the situation.
  • Then select the JDK version to build.
  • Finally, select the required dependency, input keywords such as Web, there will be relevant prompts, here I will first join the Web dependency.

After all things are finished, click the Generate Project button at the bottom, or click Alt+Enter, the Project will be automatically downloaded, unzip the downloaded Project, and then open it with IntelliJ IDEA or Eclipse for development.

Create using development tools

IntelliJ IDEA and STS are examples of IntelliJ IDEA. It should be noted that only Ultimate version of IntelliJ IDEA can directly create Spring Boot projects. The community edition does not have this feature.

IntelliJ IDEA

Start by selecting Spring Initializr when creating the project, as shown below:

Then click Next and fill in the basic information for the Maven project as follows:

Then select the dependency you want to add, as shown below:

Click Next to finish creating the project.

STS

Here I will introduce the Eclipse faction OF STS for your reference. STS created the Spring Boot project from the website in the previous section. The steps are as follows:

First right click and select New -> Spring Starter Project as shown below:

Then fill in the relevant information of the project in the opened page, as shown below:

The information here is the same as the previous one, so I won’t repeat it. Click Next to complete the project.

Maven create

In fact, the above mentioned methods all rely on the website https://start.spring.io/. Songgo remembers that in 2017, the website was not very stable and project creation failures often occurred. Since 2018, project creation failures have been rare. But some readers still occasionally encounter the problem, asking Songgo on wechat how to deal with the waist. My general advice is to use Maven directly to create projects. The steps are as follows:

First, create a normal Maven project, using IntelliJ IDEA as an example.

Note that instead of selecting the project skeleton (there are about a dozen Spring Boot-related project skeletons), click Next and fill in the Next step with the basic information for a Maven project, as shown below:

Then click Next to complete the project creation.

After creation, add the following dependencies to the POM.xml file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8. RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
Copy the code

Create a package in the Java directory and create a startup class named App as follows:

@EnableAutoConfiguration
@RestController
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    @GetMapping("/hello")
    public String hello(a) {
        return "hello"; }}Copy the code

@enableAutoConfiguration Indicates that automatic configuration is enabled.

Then execute the main method here to start a Spring Boot project.

The project structure

The project structure created by using the tool is roughly as follows:

For us, SRC is the most familiar, the Java code and configuration files are written here, the test directory is used for testing, and POM.xml is Maven’s coordinate file, and that’s it.

conclusion

This article mainly introduces three ways to create a Spring Boot project, you are welcome to discuss more methods.