Spring Boot

Spring Boot 可以轻松创建独立的、生产级的基于 Spring 的应用程序,您可以“直接运行”这些应用程序。

我们对 Spring 平台和第三方库采取了固执的观点,因此您可以轻松上手。大多数 Spring Boot 应用程序需要最少的 Spring 配置。

如果您正在寻找有关特定版本的信息,或有关如何从早期版本升级的说明。

特征

  • 创建独立的 Spring 应用程序
  • 直接嵌入Tomcat、Jetty或Undertow(无需部署WAR文件)
  • 提供自以为是的“入门”依赖项以简化您的构建配置
  • 尽可能自动配置 Spring 和 3rd 方库
  • 提供生产就绪功能,例如指标、运行状况检查和外部化配置
  • 完全不需要代码生成,也不需要 XML 配置

入门

Quickstart Guide

创建一个hello

  1. 创建项目

    image-20211004225107018

  2. 添加代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package org.akachi.springbootquickstartguide;

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

    @SpringBootApplication
    @RestController("/hello")
    public class SpringBootQuickstartGuideApplication {

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

    @GetMapping
    public String hello(@RequestParam(value="name" ,defaultValue = "spring") String name){
    return String.format("hello %s!",name);
    }
    }

Building an Application with Spring Boot

查看容器中的bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.example.springboot;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

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

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

};
}

}

添加springtest

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  • 测试代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    package org.akachi.buildinganapplicationwithspringboot;


    import static org.hamcrest.Matchers.equalTo;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

    import org.junit.jupiter.api.Test;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

    /**
    * @Author akachi
    * @Email zsts@hotmail.com
    * @Date 2021/10/5 11:07
    */
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void getHello() throws Exception {
    mvc.perform(
    MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andExpect(content().string(equalTo("Greetings from Spring Boot!"))
    );
    }
    }

  • actuator

    会显示项目健康状态

    1
    2
    http://localhost:18081/actuator
    http://localhost:18081/actuator/health

    您可以通过运行以下命令来检查应用程序的运行状况:

    1
    2
    $ curl 192.168.3.148:18081/actuator/health
    {"status":"UP"}
    1
    2
    curl -X POST 192.168.3.148:18081/actuator/shutdown
    {"timestamp":"2021-10-05T06:44:20.117+00:00","status":404,"error":"Not Found","path":"/actuator/shutdown"}[root@AKACHI-PC-2018 dell]#

spring boot starters

1
https://docs.spring.io/spring-boot/docs/2.5.0/reference/htmlsingle/#using.build-systems.starters
  • 通过spring命令启动

    安装以下内容并且配置环境变量

    1
    https://docs.spring.io/spring-boot/docs/2.5.0/reference/htmlsingle/#getting-started.installing.cli

    创建文件 app.groovy写入以下内容

    1
    2
    3
    4
    5
    6
    7
    8
    @RestController
    class ThisWillActuallyRun {

    @GetMapping("/")
    String home() {
    return "Hello, World!"
    }
    }

    执行

    1
    $ spring run app.groovy

    image-20211005161033900

    image-20211005161058448

参考资料

docs: https://spring.io/projects/spring-boot