springboot yaml part1

官方文档

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config-yaml

一、基础

1、注意事项

key:空格value

2、常用写法

# 普通 k v
name: wt

# 对象
person: 
  name: wt
  age: 24

# 数组  -空格v1
pets: 
  - dog
  - cat
  - pig

二、赋值

作用:

过程:

1、导包(不导包,不影响运行,但会报红)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2、pojo 实体类 注入

@Component

@ConfigurationProperties(prefix = "user") 参数可以通过看源码获取

3、根据pojo实体类,配置application.yaml文件

user:
  name: tom
  age: 101

4、使用

实体类注入后,需要实现自动装配

@Autowired

三、yaml语法

${random.int}

打.后,会自动提示

 四、松散绑定

 

 

 

 五、JSR303

详情网址

https://www.cnblogs.com/huanchupkblog/p/12915268.html

简单示例

1、导入jar包

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

2、在pojo类中添加注解

@Validated

3、在对应的属性添加验证的注解

@NotNull(message = "年龄不能为null")

4、案例

package com.wt.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    private String name;
    @NotNull(message = "年龄不能为null")
    private Integer age;
}

 

 

 

下面放弃  #####################################################################################

注意:配置文件放在外边

spring.config.location=配置文件路径

java -jar jia包 spring.config.location=配置文件路径

https://www.jianshu.com/p/97222440cd08

 

配置文件一般使用

导入maven

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

application.yaml

一、基础语法(不推荐行内写法)

1、存普通key: 空格vale

name: wt

2、存对象

person:
  name:wt
  age:10

行内写法

student:{name:wt, age:10}

3、数组

pets:
  - cat
  - dpg
  - pig
行内写法
pet:{cat, dog, pig}

二、使用yaml作为配置文件
实体类

package com.wt.boot.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
    private String name;
    private int age;

    public Dog() {
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

application.yaml

server:
  port: 8083

dog:
  name: 二哈
  age: 10

 测试类

package com.wt.boot;

import com.wt.boot.pojo.Dog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BootApplicationTests {
    @Autowired
    private Dog dog;

    @Test
    void contextLoads() {
        System.out.println(dog.toString());
    }

}

 三、特殊语法

${random.uuid}

四、松散绑定

userName user-name

五、JSR303

参考网址:

http://www.mamicode.com/info-detail-2905245.html1

@Validated 类的注解
@Email 属性的注解

六、config优先级

 

posted @ 2020-07-20 17:17  市丸银  阅读(164)  评论(0编辑  收藏  举报