1. 通用写法
server:
port: 8080
address:
province: gd
city: dg
town: qx
detail: xxx路xxx号
cards:
- 1001
- 1002
- 1003
2. 对象的行内写法
user: {id: 1001,name: tom,age: 18}
3. 数组的行内写法
cards: [1001,1002,1003]
4. 纯量
msg1: 'hello \n world'
msg2: "hello \n world"
5. 参数引用
name: tom
age: 18
id: 1001
person:
id: {id}
name: ${name}
age: ${age}
6. yml书写总结
yml区分大小写
yml的值前面必须有空格,用于分割
空格表示缩进,缩进的空格数相同表示同级
7. 获取yml配置文件的数据
方式1 @Value("${变量名}")
- @Value("${变量名}")
- Java类中定义的变量名可以和yml声明的变量名不同名
- 但是注解中的变量名必须和yml中声明的变量名同名
name: tom
arr01:
- 1001
- 1002
person:
name: jerry
age: 18
package com.lchh.lt.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloConfig {
@Value("${name}")
private String ymlName;
@Autowired
private Environment env;
@RequestMapping("/hello11")
public String getYmlName(){
return "hello "+ymlName;
}
}
- 访问浏览器,获取结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1518270f1b81c9cc4a2575488f15888a.png)
- 可以看出获取到配置文件的变量的值
方式2 Environment env
person:
id: 1001
name: tom
age: 18
arr:
- 1001
- 1002
- 1003
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
private Environment env;
@RequestMapping("/hello")
public String sayHello(){
String name = env.getProperty("person.name");
Integer arr01 = env.getProperty("arr[0]");
System.out.println(name );
System.out.println(arr01 );
}
方式3 @ConfigurationProperties
package com.lchh.lt.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties
@Component
public class Person {
private Integer id;
private String name;
private Integer age;
}
name: tom
person:
id: 1001
name: jerry
age: 18
package com.lchh.lt.config;
import com.lchh.lt.bean.Person;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloConfig {
@Autowired
private Environment environment;
@Autowired
private Person person;
@RequestMapping("/hello")
public String sayHello(){
System.out.println("person="+person);
return "hello";
}
}
- 控制台输出结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a420cb8dae004fdf81ede4b349f74d6e.png)
- 发现输出的结果并不是我们想要的,这个时候用到一个属性 prefix 前缀
package com.lchh.lt.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix="person")
@Component
public class Person {
private Integer id;
private String name;
private Integer age;
}
- 重新启动,然后开控制台打印结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/02aa5dfa0fc181cd750b9bcfc4086ff2.png)
8. yml配置文件获取注解提示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>