yaml配置注入

yaml基础语法

  说明:语法要求严格!

  1、空格不能省略,结束不用的分号

  2、以缩进来控制层级关系,只要是左边对齐的数据都是同一个层级的。

  3、属性和值有大小区分。

yaml注入配置文件

package com.lmh.springboot02.pojo;


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

import java.util.Date;
import java.util.List;
import java.util.Map;

//    跟application.yaml绑定
//    将配置文件的每一个属性的值映射到该类中user
//    prefix = "user" 将配置文件中的user下面的属性一一对应

@Component//注册bean
@ConfigurationProperties(prefix = "user")

public class User {
    private String name;
    private String age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object>maps;
    private List<Object>lists;
    private Dog dog;


    public User() {
    }
    public User(String name, String age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
package com.lmh.springboot02.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
 //注册bean
public class Dog {
    @Value("汤圆")
    private String name;
    @Value("3")
    private Integer age;

    public Dog() {

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

application.yaml文件

user:
  name: 咸鱼
  age: 20
  birth: 2021/11/07
  happy: false
  maps: {k1: v1,k2: v2}
  lists:
    code
    music
    girl
  dog:
    name: 旺财
    age: 3

Springboot测试类

package com.lmh.springboot02;

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

@SpringBootTest
class Springboot02ApplicationTests {
    
    @Autowired //将User自动注入进来
    private User user;

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值