Springboot读取yaml数据


读取单一数据

  1. 使用@value配合 SpEL 读取单个数据
  2. 如果数据存在多层级,依次书写层级名称即可
users:
  name: zhangsan
  age: 18
@Value("${user.name}")
private String name;
@Value("${user.age}")
private int age;

读取全部属性数据

  1. 使用Environment对象封装全部配置信息
  2. 使用@Autowired自动装配数据到Environment对象中
  3. 调用getProperty()获取数据
@AutoWired
private Enviroment env;

自定义对象封装指定数据

  1. 准备数据 application.yaml

    datasource:
      driver: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost/springboot_db
      username: root
      password: root
  2. 定义 Java Bean

    // 定义数据类型封装yamnl文件中的数据
    // 定义为Spring管控的Bean
    @Component
    // 指定加载的数据
    @ConfigurationProperties(prefix = "datasource")
    public class MyDataSource{
        private String driver;
        private String url;
        private String username;
        private String password;
    
        // Getter/Setter...
    }

文章作者: Wujiu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Wujiu !
  目录