其他分享
首页 > 其他分享> > 【Springboot学习】从零开始学习Springboot(四)

【Springboot学习】从零开始学习Springboot(四)

作者:互联网

yaml语法

数组

一种写法

#数组
like:
  - a
  - b
  - c
#对象数组
like:
  - name: hh # - 为元素
    age: 18
  - name: aa
    age: 17

 另一种简约写法

#数组
likes: [足球,篮球]
#对象数组
likes: [ { hh:a , ll:b } , { hh:c , ll:d } ]

yaml数据读取

单个数据读取

 后端代码

@RestController
@RequestMapping(value="/books")
public class BookController {

    @Value("${test1.test}")
    private int test00;
    
    @GetMapping
    public String getById(){
        System.out.println("springboot is running");
        return "springboot is running"+test00;
    }
}

yaml

//yaml
test1:
  test: 0

效果

使用Environment对象读取全部配置信息

后端代码

@RestController
@RequestMapping(value="/books")
public class BookController {

    @Value("${test5}")
    private String abc;
    
    @GetMapping
    public String getById(){
        System.out.println("springboot is running");
        System.out.println(env.getProperty("test5"));
        return "springboot is running"+abc;
    }
}

 yaml

test4: abcd
test5: ${test4}efg

效果

yaml特定属性引用

需要引用的数据

test10:
  test101: test101.
  test102: 102
  test103: test103@test.test

处理步骤

  1. 定义特定的类来封装yaml文件对应的数据
    public class MyTest {
        private String test101;
        private String test102;
        private String test103;
    
        @Override
        public String toString() {
            return "MyTest{" +
                    "test101='" + test101 + '\'' +
                    ", test102='" + test102 + '\'' +
                    ", test103='" + test103 + '\'' +
                    '}';
        }
    
        public String getTest101() {
            return test101;
        }
    
        public void setTest101(String test101) {
            this.test101 = test101;
        }
    
        public String getTest102() {
            return test102;
        }
    
        public void setTest102(String test102) {
            this.test102 = test102;
        }
    
        public String getTest103() {
            return test103;
        }
    
        public void setTest103(String test103) {
            this.test103 = test103;
        }
    }
  2. 定义为spring管控的bean
  3. 指定加载的数据
  4. 打印对象
    @RestController
    @RequestMapping(value="/books")
    public class BookController {
        @Autowired
        private  MyTest my;
    
        @GetMapping
        public String getById(){
            System.out.println("springboot is running");
            System.out.println(env.getProperty("test5"));
    
            return "springboot is running"+my;
        }
    }

测试结果

yaml属性引用

后端代码

@RestController
@RequestMapping(value="/books")
public class BookController {
    @Value("${test5}")
    private String abc;

    @GetMapping
    public String getById(){
        System.out.println("springboot is running");
        return "springboot is running"+abc;
    }
}

yaml

test4: abcd
test5: ${test4}efg

效果

 

标签:Springboot,test102,test103,test101,学习,yaml,从零开始,public,String
来源: https://www.cnblogs.com/tnxts/p/16437084.html