赞
踩
YAML(YAML Ain't Markup Language),一种数据序列化格式。
优点:
YAML文件扩展名:
核心规则:数据前面要加空格与冒号隔开
- country: china
- province: beijing
- city: beijing
- area: haidian
-
- port: 8080
-
- party: true
-
- birthday: 1949-10-01
-
- user:
- name: jf
- age: 16
-
- likes: [game,music,sleep]
-
- users:
- - name: zhangsan
- age: 18
- - name: lisi
- age: 20

Boolean: TRUE | #TRUE,true,True,FALSE,false,False均可 |
float: 3.14 | #6.8523015e+5 支持科学及算法 |
int: 123 | #0b1010_0111_0100_1010_1110 支持二进制、八进制、十六进制 |
null: ~ | #使用~表示null |
string: hello world | #字符串可以直接书写 |
string2: "hello world" | #可以使用双引号包裹特殊字符 |
date: 2022-07-29 | #日期必须使用yyyy-MM-dd格式 |
datetime: 2022-07-29T15:02:31+08:00 | #时间和日期之间使用T连接,最后使用+代表时区 |
使用@Value配合SpEL读取单个数据,属性名引用方式:${一级属性名.二级属性名...},遇到数组用中括号,如果数据存在多层级,依次书写层级名称即可,代码和效果图如下:
注意:user 默认有属性值 就是你系统 用户信息
- @RestController
- @RequestMapping("/books")
- public class BookController {
-
- //读取yaml数据中的单一数据
- @Value("${country}")
- private String country1;
-
- @Value("${user.name}")
- private String name1;
-
- }
- #使用${属性名}引用数据
- tempDir: "${baseDir}\temp"
- #使用引号包裹的字符串,其中的转义字符可以生效
- tempDir2: "${baseDir}\temp\t1\t2\t3"
注意:在yaml中是支持转义字符的,例如上图的例子只要加上引号效果就会不一样
封装全部数据到 Environment 对象
使用 @Autowired 自动装配数据到 Environment 对象中
Controller代码:
- package com.jf.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.env.Environment;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- import java.awt.print.Book;
-
- //Rest模式
- @RestController
- @RequestMapping("/books")
- public class BookController {
-
- //读取yaml数据中的单一数据
- @Value("${country}")
- private String country1;
-
- @Value("${user.name}")
- private String name1;
-
- @Value("${tempDir}")
- private String tempDir;
-
- //使用自动装配将所有的数据封装到一个对象Environment中
- @Autowired
- private Environment env;
-
- @GetMapping
- public String getById(){
- System.out.println("springboot is running...");
- System.out.println("country1===>" + country1);
- System.out.println("name1===>" + name1);
- System.out.println("tempDir===>" + tempDir);
- System.out.println("--------------------------");
- System.out.println(env.getProperty("user.name"));
- System.out.println(env.getProperty("country"));
- return "springboot is running...";
- }
- }

yml代码:
- country: china
- province: beijing
- city: beijing
- area: haidian
-
- port: 8080
-
- party: true
-
- birthday: 1949-10-01
-
- user:
- name: jf
- age: 16
-
- likes: [game,music,sleep]
-
- users:
- - name: zhangsan
- age: 18
- - name: lisi
- age: 20
-
- server:
- port: 81
-
- baseDir: c:\windows
-
- #使用${属性名}引用数据
- tempDir: "${baseDir}\temp"
- #使用引号包裹的字符串,其中的转义字符可以生效
- tempDir2: "${baseDir}\temp\t1\t2\t3"

- datasource:
- driver: com.mysql.jdbc.Driver
- url: jdbc:mysql://localhost/springboot_db
- username: root
- password: 123123
提供一个用来封装的模型类
- package com.jf;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
-
- //1.定义数据模型封装yaml文件中对应的数据
- //2.定义为spring管控的bean
- @Component
- //3.指定加载的数据
- @ConfigurationProperties(prefix = "datasource")
- public class MyDataSource {
- private String driver;
- private String url;
- private String username;
- private String password;
-
- public String getDriver() {
- return driver;
- }
-
- public void setDriver(String driver) {
- this.driver = driver;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- @Override
- public String toString() {
- return "MyDataSource{" +
- "driver='" + driver + '\'' +
- ", url='" + url + '\'' +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }

@component (把普通pojo实例化到spring容器中,相当于配置文件中的 <bean id="" class=""/>)
使用 @ConfigurationProperties 注解绑定配置信息到封装类中,封装类需要定义为Spring管理的Bean,否则无法进行属性注入。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。