# 03.SpringBoot-最佳实践

# 1.SpringBoot应用如何编写

1)引入场景依赖 ([官方文档](Build Systems :: Spring Boot (opens new window)))

2)查看自动配置了哪些(选做)

  • 自己分析,引入场景对应的自动配置一般都生效了
  • 配置文件中debug=true开启自动配置报告。
    • Negative(不生效)
    • Positive(生效)

3)是否需要修改

  • 参照文档修改配置项

  • 自定义加入或者替换组件

    • @Bean、@Component…
  • 自定义器 XXXXXCustomizer;


# 2.Lombok

Lombok用标签方式代替构造器、getter/setter、toString()等鸡肋代码。

spring boot已经管理Lombok。引入依赖:

 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
</dependency>
1
2
3
4

IDEA中File->Settings->Plugins,搜索安装Lombok插件。

示例:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Pet {
    private String name;
}
1
2
3
4
5
6

编译后的类:

public class Pet {
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Pet)) {
            return false;
        } else {
            Pet other = (Pet)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Pet;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    public String toString() {
        return "Pet(name=" + this.getName() + ")";
    }

    public Pet() {
    }

    public Pet(final String name) {
        this.name = name;
    }
}
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

也可用@Sl4j注解记录日志

@Slf4j
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(@RequestParam("name") String name){
        log.info("请求进来了....");
        return "Hello, Spring Boot 2!"+"你好:"+name;
    }
}
1
2
3
4
5
6
7
8
9

# 3.dev-tools

修改后,Ctrl+F9,自动更新,其实就相当于重启,而不是热更新

添加依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
1
2
3
4
5
6
7

# 4.yaml

1.基本语法

  • key: value;kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释
  • 字符串无需加引号,如果要加,双引号会使\n等生效,单引号会将其作为字符串输出

2.数据类型

  • 字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
1
  • 对象:键值对的集合。map、hash、set、object
#行内写法:  

k: {k1:v1,k2:v2,k3:v3}

#或

k: 
  k1: v1
  k2: v2
  k3: v3
1
2
3
4
5
6
7
8
9
10
  • 数组:一组按次序排列的值。array、list、queue
#行内写法:  

k: [v1,v2,v3]

#或者

k:
 - v1
 - v2
 - v3
1
2
3
4
5
6
7
8
9
10

3.示例

@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
} 



@Data
@NoArgsConstructor
@AllArgsConstructor
public class Pet {
    private String name;
    private Double weight;
}
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

用yaml表示以上对象

person:
  user-name: "dj \n lalala"
  boss: true
  birth: 2019/12/12 20:12:33
  age: 18
  pet:
    name: "tom"
    weight: 11.99
  interests: ["football", "basketball"]
  animal:
    - la
    - bb
  score:
    english: 100
  salarys:
    - 5000
    - 6000
  all-pets:
    tom: [{"name": "tom", "weight": 11.99}, {"name": "tom", "weight": 11.99}]
    bb:
      - name: "bb"
        weight: 11.99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

最近更新: 9/19/2026, 1:27:08 PM
编程NOTE   |