SpringBoot学习笔记6 - 读取配置文件信息

1、读取application.yml中的配置属性


假设在application.yml文件中,我们定义了如下属性:

spring:
  profiles:
    active: dev

author:
  name: blairscott # 自定义属性

---
spring:
  profiles: dev
server:
  port: 8081 # 系统属性
  servlet:
    context-path: /devpath

---
spring:
  profiles: pro
server:
  port: 8082
  servlet:
    context-path: /propath

我们期望在代码中获取到程序真正使用到的端口号,我们可以:

@RestController
public class DefController {
    @Value("${server.port}") // 获取系统配置属性
    private int port;

    @Value("${author.name}") // 获取自定义配置属性
    private String authorName;

    @GetMapping("/port")
    public String getPort() {
        return "Current Port is " + port;
    }

    @GetMapping("/authorName")
    public String getAuthorName() {
        return "The Author Name is " + authorName;
    }
}

启动程序,运行结果:

在这里插入图片描述

2、从属性文件中获取对象


2.1、说明

SpringBoot开发手册中提到:

在这里插入图片描述
文中提到,YAML文件是有缺点的,它不能以@PropertySource注解的方式被加载,若希望以该注解的方式加载这些属性,则需要使用properties属性文件。首先,我们创建custom.properties属性文件:

在这里插入图片描述

2.1、获取简单对象

创建对应测试数据

student.name=张三
student.age=27
student.score=90.5

此时,我们可以通过@Value注解的方式获取到里面的属性值。然而有的时候我们希望一次性获取到里面的所有student对应值,一次一次的取出显的很麻烦,我们更希望通过对象的方式直接获取出来,于是我们可以:

  • 首先定义一个Student类

    package com.blairscott.bean;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @PropertySource(value = "classpath:custom.properties", encoding = "UTF-8") // 指定要读取的配置文件路径
    @ConfigurationProperties("student") // 指定要读取的属性的前缀
    @Data
    public class Student {
        private String name;
        private int age;
        private double score;
    }
    
  • 编写Controller类测试

    @RestController
    public class DefController {
        @Autowired
        private Student student;
    
        @GetMapping("/studentInfo")
        public Student getStudentInfo() {
            return student;
        }
    }
    
  • 启动并查看结果

    在这里插入图片描述

2.3、获取普通数组或集合对象

  • 创建对应测试数据

    country.cities[0]=beijing
    country.cities[1]=shanghai
    country.cities[2]=guangzhou
    
  • 定义一个Country类

    package com.blairscott.bean;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    @Component
    @PropertySource("classpath:custom.properties")
    @ConfigurationProperties("country")
    @Data
    public class Country {
    
        private List<String> cities;
    
    }
    
  • 编写Controller测试类

    @RestController
    public class DefController {
        @Autowired
        private Country country;
        
        @GetMapping("/countryInfo")
        public Country getCountryInfo() {
            return country;
        }
    }
    
  • 启动并查看结果

    在这里插入图片描述

2.4、获取对象数组或集合

  • 创建对应测试数据

    company.departs[0].name=研发部
    company.departs[0].level=10
    company.departs[0].nums=9
    company.departs[1].name=教学部
    company.departs[1].level=11
    company.departs[1].nums=13
    company.departs[2].name=市场部
    company.departs[2].level=12
    company.departs[2].nums=30
    
  • 定义Company类和Department类

    @Data
    public class Department {
        private String name;
        private int level;
        private int nums;
    }
    
    @Component
    @PropertySource("classpath:custom.properties")
    @ConfigurationProperties("company")
    @Data
    public class Company {
        private List<Department> departs;
    }
    
  • 编写Controller测试类

    @RestController
    public class DefController {
        @Autowired
        private Company company;
    
        @GetMapping("/company")
        public Company getCompany() {
            return company;
        }
    }
    
  • 启动并查看结果

    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值