SpringBoot03-yml

116 阅读1分钟

SpringBoot 更加推荐yml

无需自动导入

层级很强

server:
  port: 8080
  servlet:
    context-path: /yml

image.png

属性绑定 JavaBean Properties binding

【lombok】

添加依赖

<!--配置文件属性名提示-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>
<!--在编译期间帮助生成Getter、Setter等代码-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

@ConfigurationProperties("xxx")

对应 yml里面的内容

image.png

无需@Value

image.png

取代下面的 @Value

image.png

@Data

生成getter setter toStirng 等方法 可以打印一下 Person里面的方法

image.png

image.png

安装lombok

image.png

image.png

image.png

processor

必须要编译一下哦

image.png

数组 Map形式

数组

下面的三种写法都可以

nick-names: zhangsan, lisi, wangwu
nick-names: [zhangsan, lisi, wangwu]
nick-names:
  - zhangsan
  - lisi
  - wangwu

map

image.png

数组对象

dog:
  age: 5
  name: mosi
dogs:
  - age: 11
    name: ${person.dog.name}_1
  - age: 12
    name: def

image.png

${xxx}

可以获取已经存在的

image.png

@EnableConfigurationProperties(Student.class)

在调用位置取代 @Component

image.png

image.png

构造方法绑定

cat:
  id: 1
  name: 小黑
//@Component
@ConfigurationProperties("cat")
@Data
@ConstructorBinding
public class Cat {
    private Integer id;
    private String name;

    public Cat(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
}

image.png

@ConfigurationProperties与@Bean

image.png

宽松绑定

烤肉串 我基本都是编译后 提示打代码

拆分配置文件

---

image.png

image.png

默认加载下面的

spring: profiles: active:

image.png

拆分分页 以 - 后面加名字

image.png

application-development.yml application-production.yml

image.png

继续拆文件 同时加载的

spring:
  profiles:
    active: path, port, development

image.png