get started with spring boot

The two most exciting features of spring boot are starter dependencies and auto-configuration. This article introduces how to quickly use the spring boot framework to build a spring mvc rest site, so that we can have a preliminary understanding of spring boot.

get template code

There are several ways to get template code, for example:

  • spring initializr
  • spring cli
  • Various IDE plugins

Method 1: spring initializr

Using spring initializr is the most convenient, no need to install anything. Visit http://start.spring.io/ in the browser , just fill in the relevant information, click the [generate project] button, you can download a zip file, unzip the zip file, and get a maven project (or gradle project).

Method 2: spring cli

As a developer, I prefer the command line approach. Window or Linux users, directly download https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.0.1.RELEASE/spring-boot-cli-2.0.1.RELEASE-bin .tar.gz file, after pressurizing, configure environment variables to use springcommands. Mac users can use homebrew to install spring cli more easily

$ brew tap pivotal/tap
$ brew install springboot

After the spring cli is installed, let's try it out. Let's take a look at the help of the spring command first

$ spring --help

We need to use the spring initcommand to create the parameters that can be used spring help initto view init. Enter the following command to create a project

$ spring init -g=com.example -a=spring-boot-start -d=web 
Using service at https://start.spring.io
Content saved to 'spring-boot-start.zip'

Spring generates a zip file for us, unzip the file to get a template project.

add a controller

Open this template project with the ide you are used to, and add a new class, the content is as follows

package com.example.springbootstart.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestContoller {

    @RequestMapping("/test")
    public String hello(){
        return "hello,spring boot";
    }
}

Compile, package, run

Compile and package with maven

mvn clean package

After compiling, you will get a fat jar, and a tomcat is embedded, so you can run the jar file directly

java -jar target/spring-boot-start-0.0.1-SNAPSHOT.jar

After startup, visit http://localhost:8080/test to display it hello,spring boot.

About spring boot

I wrote a controller code, nothing else was done, no configuration, and an mvc site was run. There is no shortage of packages, and the version does not conflict. This is due to spring boot. At the beginning of the article, two major features of spring boot are mentioned (in fact, there are many features, which will be mentioned in the following blog), namely: start-up dependencies and automatic configuration.

About starting dependencies

Open the pom.xml file and take a look, the content 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>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-start</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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>

The starter here is the so-called start-up dependency. It looks very simple, but the function is very powerful. It not only contains all the necessary dependencies required to run an mvc site, but also includes: embedded servlet container, fat jar, executable jar and other features. You only need to simply use it at startup java -jar. Yes, no other classpath needs to be set.

About automatic configuration

Before spring boot, if you want to use spring mvc to develop a site, you need to configure several beans. My original blog started to use spring mvc to record how to build a spring mvc site from scratch. Now, we do not configure DispatcherServlet, InternalResourceViewResolver, static file address, etc. Spring detects that the current project references the spring-mvc package, automatically registers the corresponding spring bean, and sets default values ​​for some properties. .

I will write a series of blogs about spring boot next, and try to cover more usage and details of spring boot. The series includes the following themes:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772203&siteId=291194637