Starting with this blog, we’ve moved into the world of Spring Boot, which has made Spring development much simpler and is therefore popular with many programmers.

As the first post in the Spring Boot series, we will explain how to set up a Spring Boot project.

If you use Spring Boot directly, it is recommended that you have time to learn Spring, so that you can better appreciate the convenience brought by Spring Boot.

Spring series blog: www.cnblogs.com/zwwhnly/cat… .

1. Two ways to set up a Spring Boot project

1.1 Method 1: Use the official website

First, in your browser, type start.spring. IO/and you’ll see the following interface:

As can be seen from the figure above, the default value of Project is Maven Project, the default value of Language is Java, and the default version of Spring Boot is 2.1.9. As these three items meet our requirements, we do not modify them.

Then fill in the Project information, the Project Metadata item, as follows:

For example, if the domain name is zwwhnly.com, this would be com.zwwhly.

Artifact can be understood as the project name, for example, SPRINGboot-Action.

Next, select the dependency. For example, to add a Web dependency, you can search for a Web dependency by keyword, as shown below:

Web dependencies can also be found by category, as follows:

Either way you add it, you end up with something like this:

Finally, click the “Generate” button to Generate the project code:

The decompressed code directory is as follows:

As you can see, what is generated here is a simple Maven-based project that you can open using your favorite development tool, such as IntelliJ IDEA.

1.2 Mode 2: Using IDEA

First, open the development tool IntelliJ IDEA, click the menu File–New–Project to open the dialog box of New Project, first select “Spring Initializr” on the left, then select the JDK version to be used for the Project, such as 1.8, and click “Next” button:

Fill in the project information, packaging method, Java version and other information as prompted in the picture below, and click “Next” button:

Select the dependencies to be used by the project (such as Spring Web) and the version of Spring Boot (such as 2.1.9), and click the “Next” button:

Confirm the project name and the path to save the project, click “Finish” button, if the saved path does not exist, it will prompt you to automatically create the directory, click” OK “:

The created project structure diagram is as follows:

The Maven dependency tree looks like this:

2. Pom. XML

The default generated POM.xml file looks like this:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.zwwhnly</groupId>
    <artifactId>springboot-action</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>springboot-action</name>
    <description>Spring Boot Action Code</description>

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

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

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

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

</project>
Copy the code

For a full explanation of POM.xml, refer to my previous blog: Getting Started with Spring (part 4) : Managing Spring Projects with Maven, but let’s just cover some of the differences.

The first thing worth noting is the parent tag in the file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9. RELEASE</version>
    <relativePath/> <! -- lookup parent from repository -->
</parent>
Copy the code

This tag is used to add Spring Boot’s parent dependencies, where spring-boot-starter-parent is a special starter that provides the associated Maven default dependencies, eliminating the version tag from common package dependencies.

As a concrete example, we add the following dependencies to pom.xml:

<dependency>
   <groupId>com.rabbitmq</groupId>
   <artifactId>amqp-client</artifactId>
</dependency>
Copy the code

As with the default two dependencies, we do not specify the version of the dependency, but we can see the versions used by the three dependencies in Maven’s dependency tree, as shown below:

This is what the parent tag references spring-boot-starter-parent for. You can check the following files to see which dependencies it provides:

In this file, you can find the version information specified by spring-boot-starter-web, spring-boot-starter-test, and amqp-client:

<properties>
    <rabbit-amqp-client.version>5.4.3</rabbit-amqp-client.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>${rabbit-amqp-client.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.9. RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.1.9. RELEASE</version>
    </dependency>
</dependencies>
Copy the code

You can see that the specified version is the version information seen in the Maven dependency tree.

The default version can be overridden by specifying the version version. For example, we change the dependency information of amQp-client to:

<dependency>
   <groupId>com.rabbitmq</groupId>
   <artifactId>amqp-client</artifactId>
   <version>5.7.0</version>
</dependency>
Copy the code

The Maven dependency tree shows the amQp-client version as 5.7.0:

The second thing to note is that a compiler plug-in for Spring Boot has been added to the file:

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
   </plugins>
</build>
Copy the code

Here we also do not specify version, so we use the default version 2.1.9.RELEASE (same as using amQp-client above) :

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.1.9. RELEASE</version>
</plugin>
Copy the code

3. Simple demo

Find start class SpringbootActionApplication (name is commonly ArtifactId + Application), see the default code is as follows:

package com.zwwhnly.springbootaction;

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

@SpringBootApplication
public class SpringbootActionApplication {

    public static void main(String[] args) { SpringApplication.run(SpringbootActionApplication.class, args); }}Copy the code

For demonstration purposes, we have temporarily added an Api to the startup class:

package com.zwwhnly.springbootaction;

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

@RestController
@SpringBootApplication
public class SpringbootActionApplication {

    @RequestMapping("/")
    public String index(a) {
        return "Hello Spring Boot";
    }

    public static void main(String[] args) { SpringApplication.run(SpringbootActionApplication.class, args); }}Copy the code

Right-click in the Startup class and select Run Project to see the following:

If you visit http://localhost:8080/ in your browser, you will see the following information:

The @RestController and @RequestMapping annotations are related to Spring MVC and are not specific to Spring Boot. For more information about Spring MVC, check out the following blog:

  1. Getting Started with Spring: How to use Spring MVC
  2. Getting started with Spring (13) : Explaining common Spring MVC annotations
  3. Getting started with Spring (xiv) : 2 ways to test Spring MVC controllers

4. Close the Banner

We have seen the Spring Boot Banner when we started the project, so you can’t help asking what is the Spring Boot Banner?

It’s actually the place marked in red below:

One might think that displaying this information every time you start up doesn’t really make sense, so how do we turn it off?

4.1 Close by code

The original code:

public static void main(String[] args) {
    SpringApplication.run(SpringbootActionApplication.class, args);
}
Copy the code

Revised:

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

    SpringApplication springApplication = new SpringApplication(SpringbootActionApplication.class);
    springApplication.setBannerMode(Banner.Mode.OFF);
    springApplication.run(args);
}
Copy the code

4.2 Using the Configuration file

By default, new Spring Boot projects have an empty application.properties configuration file that can be configured as follows:

spring.main.banner-mode=off
Copy the code

Restart the project as shown below:

5. Source code and reference

Source code address: github.com/zwwhnly/spr… Welcome to download.

Java EE Development Disruptor: Spring Boot Combat by Wang Yunfei

Create the first Springboot project using IDEA

Spring Boot- Disables Banner

Example Disable the Spring Boot banner

SpringBoot yml configuration