亲宝软件园·资讯

展开

SpringBoot yaml语法

不会压弯的小飞侠 人气:0

yaml

YAML是一种数据序列化格式。

yaml扩展名

yaml语法规则

字面值表示方式:

boolean: true
float: 3.14
int: 15
#表示空
null: ~
string: xiaofeixia
date: 2022-7-9
#日期与时间用T连接
datetime: 2022-7-9T12:00:30+02:00

数组表示方式:

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

对象数组格式:

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

对象数组缩略格式:

user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]

读取yaml数据

使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名}

编写yaml文件

server:
  port: 81
country: china
province: henan
city: zhengzhou
area: shangqiu

party: true
birthday: 2022-11-11

user8:
  name: xiaofeixia
  age: 22
user1:
  name: xiaofeixia
  age: 22

a:
  B:
    C:
      d:
        e: abc

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]

读取单一数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    //读取yaml数据中的单一数据
    @Value("${country}")
    public String country1;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");
        System.out.println("country=="+country1);  //country==china      
        return "springboot is running...";
    }
}

读取二级数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Value("${user8.name}")
    public String username;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("username=="+username);         //username==xiaofeixia            
        return "springboot is running...";
    }
}

读取数组数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
 @Value("${likes[0]}")
    public String likes1;
     @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("likes1=="+likes1);  //likes1==music      
        return "springboot is running...";
    }
}

读取服务器端口号

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
   @Value("${server.port}")
    public String port;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("port=="+port);  //port==81   
        return "springboot is running...";
    }
}

读取对象属性

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Value("${user2[0].age}")
    public String age2;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
         System.out.println("age2=="+age2);  //age2==22         
        return "springboot is running...";
    }
}

封装全部数据到Environment对象

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Autowired
    private Environment env;
    @GetMapping
    public String ById(){        
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user8.name"));        
        return "springboot is running...";
    }
}

读取yaml引用类型属性数据

application.yml

server:
  port: 81
#创建类用于封装下面的数据
#由spring去加载数据到对象中,一定要告诉spring加载这组信息
#使用的时候直接从spring中获取信息
datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot
  username: root
  password: root

MyDataSource

自定义对象封装指定数据

1.定义数据模型封装yaml文件中对应的数据

package com.jkj;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//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 + '\'' +
                '}';
    }
}

读取数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Autowired
    private MyDataSource myDataSource;
    @GetMapping
    public String ById(){     
        System.out.println(myDataSource);
        //MyDataSource{driver='com.mysql.jdbc.Driver', url='jdbc:mysql://localhost/springboot', username='root', password='root'}
        return "springboot is running...";
    }
}

变量的引用

application.yml

server:
  port: 81
baseDir: E:\window
tempDir: ${baseDir}\temp

读取数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/yamlBooks")
public class BookController {
     @Value("${tempDir}")
     public String temp;   
    @GetMapping
    public String ById(){     
         System.out.println("temp=="+temp);  //temp==E:\window\temp
        return "springboot is running...";
    }
}

context-path

只写:

server:
  port: 81

控制台输出:path为空

加上context-path后:

server:
  port: 81
  servlet:
    context-path: /test

控制台输出页面:

注意:在浏览器输入:http://localhost:81/test/yamlBooks 进行测试。

@Autowired报错解决方案

加载全部内容

相关教程
猜你喜欢
用户评论