文章目录
1. 通用写法
- 示例如下:
# 变量名 然后换行缩进(不能使用Tab键缩进),子变量名,然后英文冒号: 然后 空格 然后 值
server:
port: 8080
address:
province: gd
city: dg
town: qx
detail: xxx路xxx号
# 数组的写法
cards:
- 1001
- 1002
- 1003
2. 对象的行内写法
# key: value之间的空格不能省略,对象行内写法大括号必须有
user: {id: 1001,name: tom,age: 18}
3. 数组的行内写法
# 数组必须用中括号括起来
cards: [1001,1002,1003]
4. 纯量
# 单引号不能识别转移字符, \n会直接输出 \n
msg1: 'hello \n world'
# 双引号可以识别转移字符, 对应的值会分两行输出
msg2: "hello \n world"
5. 参数引用
name: tom
age: 18
id: 1001
# person对象引用上面三个参数的值,作为自己的属性的值
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;
}
}
- 访问浏览器,获取结果
- 可以看出获取到配置文件的变量的值
方式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
- 定义实体bean Person
package com.lchh.lt.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data //自动注入get set toString 等方法
@ConfigurationProperties
@Component //注入到spring容器
public class Person {
private Integer id;
private String name;
private Integer age;
}
- 定义yml配置文件
name: tom
person:
id: 1001
name: jerry
age: 18
- 获取yml文件中的值
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";
}
}
- 控制台输出结果
- 发现输出的结果并不是我们想要的,这个时候用到一个属性 prefix 前缀
package com.lchh.lt.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data //自动注入get set toString 等方法
@ConfigurationProperties(prefix="person") //带上前缀声明
@Component //注入到spring容器
public class Person {
private Integer id;
private String name;
private Integer age;
}
- 重新启动,然后开控制台打印结果
8. yml配置文件获取注解提示
- pom.xml文件添加依赖
<!--配置springboot注解提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
版权声明:本文为qq28129019原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。