上一篇:05-Springboot构造器绑定:yum文件和Properties类的构造器绑定https://blog.csdn.net/fsjwin/article/details/109700066
yml中配置的数据也可以在@Bean标注的类上,示例如下:
1. application.yml
ymlother:
str: 普通字符串
specialStr: "换\n行字符串"
num: 520
dnum: 520:22
birth: 2020/11/11 11:11:11
list:
- 大鸟
- 小鸟
- 中鸟
set:
- set1
- set2
- set3
#set: [set3,set5,set6]
map: {k1: v1, k2: v2}
users:
- name: yuhl
age: 20
- name: rll
age: 18
2. YmlService
package com.yuhl.service;
import com.yuhl.YmlOtherProperties;
import com.yuhl.YmlProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
/**
* @author yuhl
* @Date 2020/11/14 11:09
* @Classname YmlService
* @Description TODO
*/
@Service //不用标注@configuration就可以定义bean了 2.1的新特性
public class YmlService {
@Bean
@ConfigurationProperties(prefix = "ymlother")
public YmlOtherProperties ymlOtherProperties(){
return new YmlOtherProperties();
}
}
3. HelloController
@RequestMapping("/ymlOther")
public YmlOtherProperties ymlOther() {
System.out.println("ymlOtherProperties:" +ymlOtherProperties);
return ymlOtherProperties;
}