首页 > 编程语言 > 详细

springboot 配置文件yaml

时间:2020-07-20 20:09:23      阅读:77      评论:0      收藏:0      [点我收藏+]

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优先级
技术分享图片

 

springboot 配置文件yaml

原文:https://www.cnblogs.com/wt7018/p/13346187.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!